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的影响。