怎么使用Python matplotlib.pyplot.hist()绘制直方图
导读:本文共7870.5字符,通常情况下阅读需要26分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要:希望大家仔细阅读,能够学有所成!一、matplotlib.pyplot.hist()语法hist(x,bins=None,range=None,density=False,weights=None,cumulative=False,bottom=None,histtype='bar',align='mid',orientation='vertical... ...
目录
(为您整理了一些要点),点击可以直达。希望大家仔细阅读,能够学有所成!
一、matplotlib.pyplot.hist()语法
hist(x,bins=None,range=None,density=False,weights=None,cumulative=False,
bottom=None,histtype='bar',align='mid',orientation='vertical',rwidth=None,
log=False,color=None,label=None,stacked=False,*,data=None,**kwargs)
plt.hist(
x,#指定要绘制直方图的数据
bins,#设置长条形的数目
range,#指定直方图数据的上下界,默认包含绘图数据的最大值和最小值(范围)
density=TrueorFalse,#如果"True",将y轴转化为密度刻度默认为None
weights,#该参数可为每一个数据点设置权重
cumulative=TrueorFalse,#是否需要计算累计频数或频率默认值False
bottom=0,#可以为直方图的每个条形添加基准线,默认为0
histtype={'bar','barstacked','step','stepfilled'}#设置样式bar柱状形数据并排,默认值。
barstacked在柱状形数据重叠并排(相同的在一起)
step柱状形颜色不填充
stepfilled填充的线性
align='mid'or'left'or'right',#设置条形边界值的对其方式,默认为mid,除此还有'left'和'right'
orientation={'vertical','horizontal'},#设置直方图的摆放方向,默认为垂直方向vertical
rwidth,#设置直方图条形宽度的百分比
log=TrueorFalse,#是否需要对绘图数据进行log变换默认值False
color='r',#设置直方图的填充色
label,#设置直方图的标签
stacked=TrueorFalse,#当有多个数据时,是否需要将直方图呈堆叠摆放,默认False水平摆放;
facecolor,#设置长条形颜色(和color效果一致,设置color就不用再设置facecolor)
edgecolor,#设置边框的颜色
alpha#设置透明度
)注意组距,得到满意的展示效果
注意y轴所代表的变量是频数还是频率
二、绘制直方图
①绘制简单直方图
importmatplotlib.pyplotaspltimportnumpyasnp
data=np.random.randint(140,180,200)
bins设置长条形的数目
plt.hist(data,bins=10)
plt.show()
②:各个参数绘制的直方图
(1)histtype参数(设置样式bar、barstacked、step、stepfilled)
1. bar:柱状形数据并排(因为bar是默认值,可以不写)
importmatplotlib.pyplotasplt
importnumpyasnpdata=np.random.randint(140,180,200)
plt.hist(data,bins=10)
plt.show()
2. barstacked:在柱状形数据重叠并排(相同的在一起)
importmatplotlib.pyplotasplt
importnumpyasnpdata=np.random.randint(140,180,200)
plt.hist(data,bins=10,histtype='barstacked')
plt.show()
3.step:柱状形颜色不填充
importmatplotlib.pyplotasplt
importnumpyasnpdata=np.random.randint(140,180,200)
plt.hist(data,bins=10,histtype='step')
plt.show()
4.stepfilled:生成一个默认填充的线图
importmatplotlib.pyplotasplt
importnumpyasnpdata=np.random.randint(140,180,200)
plt.hist(data,bins=10,histtype='stepfilled')
plt.show()
(2)range参数(指定直方图数据的上下界,默认包含绘图数据的最大值和最小值(范围))
不想显示数据全部范围,只想查看数据某一个范围内的数据。(例:下图数据范围为140~180之间,只想查看150~170之间的数据)
importmatplotlib.pyplotasplt
importnumpyasnpdata=np.random.randint(140,180,200)
plt.hist(data,bins=10,histtype='bar',range=(150,170))
plt.show()
(3)orientation参数 (设置直方图的摆放位置,vertical垂直方向 horizontal水平方向,默认值:vertical垂直方向)
垂直方向(默认垂直,可以不写):
importmatplotlib.pyplotasplt
importnumpyasnpdata=np.random.randint(140,180,200)
plt.hist(data,bins=10)
plt.show()
horizontal水平方向:
importmatplotlib.pyplotasplt
importnumpyasnpdata=np.random.randint(140,180,200)
plt.hist(data,bins=10,orientation='horizontal')
plt.show()
(4)density参数(bool值,True:将坐标轴转化为密度刻度,默认值:None)
直方图为垂直方向时,观察y轴:
importmatplotlib.pyplotasplt
importnumpyasnpdata=np.random.randint(140,180,200)
plt.hist(data,bins=10,density=True)
plt.show()
直方图为水平方向时,观察x轴:
importmatplotlib.pyplotasplt
importnumpyasnpdata=np.random.randint(140,180,200)
plt.hist(data,bins=10,orientation='horizontal',density=True)
plt.show()
(5)weights参数(为每个数据点设置权重)
直方图为垂直方向时,观察y轴:
importmatplotlib.pyplotasplt
importnumpyasnpdata=np.random.randint(140,180,200)
plt.hist(data,bins=10,weights=data)
plt.show()
直方图为水平方向时,观察x轴:
importmatplotlib.pyplotasplt
importnumpyasnpdata=np.random.randint(140,180,200)
plt.hist(data,bins=10,orientation='horizontal',weights=data)
plt.show()
(6)cumulative参数(bool值,是否需要计算累计频数或频率,默认值:False)
频数:指事件发生的次数
频率:指次数占总次数n的比例
频率=频数/n
直方图为垂直方向时:
importmatplotlib.pyplotasplt
importnumpyasnpdata=np.random.randint(140,180,200)
plt.hist(data,bins=10,cumulative=True)
plt.show()
直方图为水平方向时:
importmatplotlib.pyplotasplt
importnumpyasnpdata=np.random.randint(140,180,200)
plt.hist(data,bins=10,orientation='horizontal',cumulative=True)
plt.show()
(7)bottom参数(为直方图添加基准线)
直方图为垂直方向时,观察y轴:
importmatplotlib.pyplotasplt
importnumpyasnpdata=np.random.randint(140,180,200)
plt.hist(data,bins=10,bottom=170)
plt.show()
直方图为水平方向时,观察x轴:
importmatplotlib.pyplotasplt
importnumpyasnpdata=np.random.randint(140,180,200)
plt.hist(data,bins=10,orientation='horizontal',bottom=170)
plt.show()
(8)align参数(设置条形边界值的对其方式,mid、left、right,默认值:mid)
mid(默认值可以不写):
importmatplotlib.pyplotasplt
importnumpyasnpdata=np.random.randint(140,180,200)
plt.hist(data,bins=10)
plt.show()
left:
importmatplotlib.pyplotasplt
importnumpyasnpdata=np.random.randint(140,180,200)
plt.hist(data,bins=10,align='left')
plt.show()
right:
importmatplotlib.pyplotasplt
importnumpyasnpdata=np.random.randint(140,180,200)
plt.hist(data,bins=10,align='right')
plt.show()
(9)rwidth参数(设置直方图条形宽度的百分比)
importmatplotlib.pyplotasplt
importnumpyasnpdata=np.random.randint(140,180,200)
plt.hist(data,bins=10,rwidth=0.5)
plt.show()
(10)log参数(bool值,对绘图数据进行log变换 默认值:False)
直方图为垂直方向时,观察y轴:
importmatplotlib.pyplotasplt
importnumpyasnpdata=np.random.randint(140,180,200)
plt.hist(data,bins=10,log=True)
plt.show()
直方图为水平方向时,观察x轴:
importmatplotlib.pyplotasplt
importnumpyasnpdata=np.random.randint(140,180,200)
plt.hist(data,bins=10,orientation='horizontal',log=True)
plt.show()
(11)stacked参数(bool值,当有多个数据时,是否需要将直方图呈堆叠摆放,默认值:False水平摆放)
stacked=False时:(水平摆放)
importmatplotlib.pyplotasplt
importnumpyasnpx=np.random.randint(140,180,200)
y=np.random.randint(140,180,200)plt.hist([x,y],bins=10)
plt.show()
stacked=True时:(堆叠摆放)
importmatplotlib.pyplotasplt
importnumpyasnpx=np.random.randint(140,180,200)
y=np.random.randint(140,180,200)plt.hist([x,y],bins=10,stacked=True)
plt.show()
(12)直方图所有参数展示:
importmatplotlib.pyplotasplt
importnumpyasnpplt.rcParams['font.sans-serif']=['FangSong']
fig=plt.figure(figsize=(8,8))
data=np.random.randint(140,180,200)data数据
bins设置长条形的个数
histtype设置样式barstacked:在柱状形数据重叠并排(相同的在一起)
range显示范围
cumulative累计频数
align设置边界对齐值为中心对齐
orientation设置摆放方向为horizontal水平方向
rwidth设置长条形宽度的百分比为20
color设置长条形的填充颜色为#FFB6C1
label设置直方图的标签
edgecolor设置长条形边框线为#FFD700
alpha设置长条形的透明度为0.5
density=True长条形呈水平方向:density将x轴转换为密度刻度长条形呈垂直方向:density将y轴转换为密度刻度
weights=data为每个数据点设置权重
bottom设置基准线为15000
log=True是否对数据进行log转换
plt.hist(data,bins=10,histtype='barstacked',range=(140,170),cumulative=True,align='mid',orientation='horizontal',rwidth=20,color='#FFB6C1',
label='数量',edgecolor='#FFD700',alpha=0.5,weights=data,bottom=10000,log=False)plt.xticks(size=20)#x轴刻度值大小
plt.yticks(size=20)#y轴刻度值大小plt.title('hist',size=30)#设置直方图标签
plt.xlabel('x轴',size=15)#设置x轴标签
plt.ylabel('y轴',size=20)#设置y轴标签plt.rcParams.update({'font.size':20})#修改图例字体大小
plt.legend()
plt.show()
三、在直方图上画折线图
importmatplotlib.pyplotasplt
importnumpyasnpx=np.random.normal(100,15,10000)
y=np.random.normal(80,15,10000)density=True设置为密度刻度
n1,bins1,patches1=plt.hist(x,bins=50,density=True,color='#00B8B8',alpha=1)
n2,bins2,patches2=plt.hist(y,bins=50,density=True,color='r',alpha=0.2)plt.plot(bins1[:-1],n1,':',lw=3)
plt.plot(bins2[:-1],n2,'--',lw=3)plt.show()
怎么使用Python matplotlib.pyplot.hist()绘制直方图的详细内容,希望对您有所帮助,信息来源于网络。