2014-09-28 74 views
1

我想创建数据来绘制python中的直方图。数据应该是分箱和值格式。
例如,输入数据: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?

+2

如果你使用'numpy',请参阅[这里](HTTP://docs.scipy .ORG/DOC/numpy的/参照/生成/ numpy.histogram.html)。 – gongzhitaao 2014-09-28 01:58:25

回答

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) 
0

我认为类CounterCollections可能会帮助你。 您可以参考pygal动态SVG图表库。