matplotlib中的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,*,data = None,** kwargs )
主要参数说明:
参数 | 参数说明 |
---|---|
x | 输入值(数组或序列) |
bins | 图形的边缘(可选) |
range | 横坐标的上限和下限(可选) |
density | 如果True,返回元组的第一个元素将被归一化以形成概率密度的计数,(可选) |
weights | x的权重 |
cumulative | 如果True,则计算直方图,其中每个bin给出该bin中的计数加上较小值的所有bin。最后一个bin给出了数据点的总数。 |
bottom | 每个bin的底部基线的位置 |
histtype | 要绘制的直方图的类型 |
align | 控制直方图的绘制方式 |
rwidth | 条的相对宽度 |
log | 如果True,直方图轴将设置为对数刻度。 |
color | 颜色规格或颜色规格序列,每个数据集一个 |
label | 字符串或匹配多个数据集的字符串序列 |
stacked | 如果True,多个数据堆叠在一起如果 False多个数据并排排列 |
随机生成一个柱状图:
import matplotlib.pyplot as plt
import numpy as np
x = np.random.normal(size=100)
plt.hist(x)
plt.show()
随机生成更复杂的柱状图:
import matplotlib.pyplot as plt
import numpy as np
plt.figure()
x = np.random.gamma(3,4,200)
plt.subplot(221)
plt.hist(x,bins = 30)
plt.subplot(222)
plt.hist(x,bins = 30,normed=True)
plt.subplot(223)
plt.hist(x,bins = 30,cumulative=True)
plt.subplot(224)
plt.hist(x,bins = 30,normed=True,cumulative=30,histtype="step")
plt.show()
("The only person standing in your way is you."--《Black Swan》
)