2014-09-28 59 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圖表庫。