我想创建用于在python中绘制直方图的数据。数据应采用分箱和数值格式 例如,输入数据:
a = [10,30,12.5,70,76,90,96,55,44.5,67.8,76,88]
我希望以表格格式输出,例如,
箱数据
10 1
20 1
30 1
40 0
50 1
60 1
70 2
80 2
90 2
100 1
我怎么能在python中这样做?
答案 0 :(得分:0)
我认为Counter
中的课程Collections
可能会对您有所帮助。
您可以参考pygal动态SVG图表库。
答案 1 :(得分:0)
如果您不想使用任何外部模块并自行编码,请使用类似的内容:
import math # You need this to perform extra math function, it is already built in
def histogram(lst): # Defining a function
rounded_list = [(int(math.ceil(i / 10.0)) * 10) for i in lst]
# Rounding every value to the nearest ten
d = {} # New dictionary
for v in xrange(min(rounded_list),max(rounded_list)+10,10):
d[v] = 0
# Creating a dictionary with every ten value from minimum to maximum
for v in rounded_list:
d[v] += 1
# Counting all the values
for i in sorted(list(d.keys())):
print ("\t".join([str(i),str(d[i])]))
# Printing the output
a = [10,30,12.5,70,76,90,96,55,44.5,67.8,76,88]
#Calling the function
histogram(a)