1、hist函數
matplotlib.pyplot.hist( x, bins=None, range=None, density=None, weights=None, cumulative=False, bottom=None, histtype='bar', align='mid', orientation='vertical', rwidth=None, log=False, color=None, label=None, stacked=False, normed=None, hold=None, data=None, **kwargs)
2、參數
x : (n,) n維數組或者n維數組序列,多維數組長度不要求一致html
bins : 整數,序列,或者 ‘auto’, 可選api
若是是整數,按bins + 1個組計算數組
若是是序列實例:[1, 2, 3, 4]app
第一個bin [1, 2) 第二個bin [2, 3) 第三個bin [3, 4]dom
若是auto:rcParam hist.bins函數
range : 元組,可選bins的邊界,若是bins是一個序列則無效若是沒有則是(x.min(), x.max())code
density : boolean, 能夠,若是爲真返回第一個值是每一個區間的百分比,默認是個數orm
weights : n維數組(n, ),可選,和數據x一致,每個數據的權重htm
cumulative : boolean, 計算每個集合的累加值對象
bottom : 標量數組,距離底邊的高度
histtype : {‘bar’, ‘barstacked’, ‘step’, ‘stepfilled’}, 默認"bar",
align : {‘left’, ‘mid’, ‘right’}, 可選,對齊方式,默認"mid"
orientation : {‘horizontal’, ‘vertical’}, 可選,bar方向
rwidth : 標量,bar的寬度,可選
log : boolean, 可選,y座標是否使用科學計數法
color : color或者color數組,設置bar顏色,color數組不是設置每個bar的顏色
label : string,圖例標籤
stacked : boolean, 可選是否垂直重疊,默認水平重疊
normed : bool, 不被推薦,使用density代替
3、返回值
n : 數組或數組列表每個bar區間的數量或者百分比
bins : 數組,bar的範圍和bins參數含義同樣
patches : 列表 或者列表的列表 圖形對象
4、實例
各個參數對比
#-*-coding:utf-8-*- import numpy as np import matplotlib import matplotlib.mlab as mlab import matplotlib.pyplot as plt import random bottom = [1, 1, 2, 3] data = [] random.seed(123456) for x in range(20): data.append(random.randint(1,5)) # print data np.random.seed(20180408) weight = np.random.rand(20) ax = plt.subplot(331) plt.hist(data,bins=4,histtype='bar',rwidth=0.8) ax.set_title("bins=4,histtype='bar',rwidth=0.8") ax = plt.subplot(332) plt.hist(data,bins=5,rwidth=0.1) ax.set_title("bins=5,rwidth=0.1") ax = plt.subplot(333) ax.set_title("bins=5,rwidth=0.3,density=True") # plt.xlabel(u'數量',fontsize=8) plt.ylabel(u'佔比',fontsize=8) n,edgeBin,patches = plt.hist(data,bins=5,rwidth=0.3,density=True) # n,edgeBin,patches = plt.hist(data,bins=5,histtype='bar',rwidth=0.8) print n print edgeBin ax = plt.subplot(334) ax.set_title("bins=5,rwidth=0.3,density=True,weights=weight") n,edgeBin,patches = plt.hist(data,bins=5,rwidth=0.3,density=True,weights=weight) # n,edgeBin,patches = plt.hist(data,bins=5,rwidth=0.3,weights=weight) print n print edgeBin ax = plt.subplot(335) ax.set_title("bins=4,rwidth=0.3,bottom=bottom") plt.hist(data,bins=4,rwidth=0.3,bottom=bottom) ax = plt.subplot(336) ax.set_title("bins=4,rwidth=0.3,histtype='stepfilled'") # plt.hist(data,bins=4,rwidth=0.8,histtype='barstacked') # plt.hist(data,bins=4,rwidth=0.8,histtype='step') plt.hist(data,bins=4,rwidth=0.3,histtype='stepfilled') colors = "rgmbc" ax = plt.subplot(337) ax.set_title("bins=4,rwidth=0.3") n,edgeBin,patches = plt.hist(data,bins=4,rwidth=0.3) random.seed() for patch in patches: patch.set_facecolor(random.choice(colors)) label = "Label" ax = plt.subplot(338) ax.set_title("bins=4,rwidth=0.3,label=label") plt.hist(data,bins=4,rwidth=0.3,label=label) plt.legend(fontsize=12) ax = plt.subplot(339) ax.set_title("bins=4,rwidth=0.3,log=True,cumulative=True") n,edgeBin,patches = plt.hist(data,bins=4,rwidth=0.3,log=True,cumulative=True) print n print edgeBin fig = plt.gcf() fig.set_size_inches(12, 10) fig.savefig("hist.png") plt.show()
這裏說明幾個參數,第一個參數x數據集沒有什麼說的,第二個參數和返回值的bins,咱們知道柱狀圖是有不少矩形組成,咱們把這些矩形稱爲"bar",bins就是描述這些"bar"的區間的。例如:bins=[1,2,3,4]就是把數據集劃分爲[1,2),[2,3),[3,4]這3個區間,每個區間表示的是數據集中落入這個區間的值的個數。若是設置了density=True,每個區間表示的就是數據集落入這個區間值個數的百分比。若是設置了cumulative =True就每個區間表示的是數據集落入這個區間的值的總和。
weights參數是設置的每個值的權重,不管是算個數,仍是算百分比,仍是算總和,weights都會參與計算。
多數據集對比
#-*-coding:utf-8-*- import numpy as np import matplotlib import matplotlib.mlab as mlab import matplotlib.pyplot as plt # np.random.seed(20180410) # data = np.random.randint(1,6,size=(20,2)) data = [[1,2,2,3,3,3,4,4,4,4,5,5,5,5,5],[5,4,4,3,3,3,2,2,2,2,1,1,1,1,1]] color = ["m","c"] ax = plt.subplot(221) ax.set_title("bins=4,rwidth=0.3,color=color") ns,edgeBin,patches = plt.hist(data,bins=4,rwidth=0.3,color=color) for n in ns: print n print edgeBin ax = plt.subplot(222) plt.hist(data,bins=4,rwidth=0.3,stacked=True) ax.set_title("bins=4,rwidth=0.3,stacked=True") ax = plt.subplot(223) plt.hist(data,bins=4,rwidth=0.3,stacked=True,align="right") ax.set_title("bins=4,rwidth=0.3,stacked=True,align=right") ax = plt.subplot(224) plt.hist(data,bins=4,rwidth=0.3,orientation="horizontal") ax.set_title("bins=4,rwidth=0.3,orientation=horizontal") fig = plt.gcf() fig.set_size_inches(12, 8) fig.savefig("hist2.png") plt.show()
這裏咱們也能夠看出來color數組是針對多數據集的。
cumulative和density對比
#-*-coding:utf-8-*- import numpy as np import matplotlib import matplotlib.mlab as mlab import matplotlib.pyplot as plt data = [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5] data2 = [[1,2,2,3,3,3,4,4,4,4,5,5,5,5,5],[5,4,4,3,3,3,2,2,2,2,1,1,1,1,1]] ax = plt.subplot(321) ax.set_title("bins=4,rwidth=0.3,cumulative=True") ns,edgeBin,patches = plt.hist(data,bins=4,rwidth=0.3,cumulative=True) for n in ns: print n print edgeBin print "---------------------------" ax = plt.subplot(322) ax.set_title("bins=4,rwidth=0.3") ns,edgeBin,patches = plt.hist(data,bins=4,rwidth=0.3) for n in ns: print n print edgeBin print "---------------------------" ax = plt.subplot(323) ax.set_title("bins=4,rwidth=0.3,density=True,cumulative=True") ns,edgeBin,patches = plt.hist(data,bins=4,rwidth=0.3,cumulative=True,density=True) for n in ns: print n print edgeBin print "---------------------------" ax = plt.subplot(324) ax.set_title("bins=4,rwidth=0.3,density=True") ns,edgeBin,patches = plt.hist(data,bins=4,rwidth=0.3,density=True) for n in ns: print n print edgeBin print "---------------------------" ax = plt.subplot(325) ax.set_title("bins=4,rwidth=0.3,cumulative=True") ns,edgeBin,patches = plt.hist(data2,bins=4,rwidth=0.3,cumulative=True) for n in ns: print n print edgeBin print "---------------------------" ax = plt.subplot(326) ax.set_title("bins=4,rwidth=0.3") ns,edgeBin,patches = plt.hist(data2,bins=4,rwidth=0.3) for n in ns: print n print edgeBin fig = plt.gcf() fig.set_size_inches(12, 10) fig.savefig("hist3.png") plt.show()
上面的主要是看輸出,從上面咱們能夠看出density對cumulative的影響。