使用matplotlib制作“直方图”:.hist(x,bins,range,label,histtype,density,rwidth,color,orientation,stacked)

有时我们想要了解一个包含许多样本的大型数据集,而不仅仅知道平均值,中位数或标准差的基本指标。为了更直观地了解数据集,我们可以使用直方图来显示所有值。

  • 展示直方图Histograms,使用.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)

参数说明:

(1)x : (n,) n维数组或者n维数组序列,多维数组长度不要求一致
(2)bins : 整数,序列,或者 ‘auto’, 可选

pyplot.hist(x, bins=5):有参数bins,会将数据集按照bins的个数统计;没有bins参数时,默认将数据集分成10个bin展示

(3)rang=(x1,x2) : 可选bins的边界,或者选择统计数据集中数据的范围[x1,x2),抑或理解成X轴展示范围
(4)align : 表示柱和刻度的对齐方式,有{‘left’, ‘mid’, ‘right’}, 可选,默认"mid"
(5)rwidth : bar的宽度
(6)color :设置bar颜色
(7)orientation : bar方向,有{‘horizontal’, ‘vertical’}可选
(8)stacked : 水平重叠还是垂直重叠,默认“False”水平重叠
(9)label : 图例标签
(10)cumulative : 计算每一个集合的累加值,boolean类型,默认值为False

返回值:

(1)n : 数组或数组列表每一个bar区间的数量或者百分比
(2)bins : 数组,bar的范围和bins参数含义一样
(3)patches : 列表 或者列表的列表 图形对象

.hist方法的直方图画法,先查找数据集中的最小值和最大值,并在这些值之间创建bins个等间距的区间,然后统计每个区间中的数据数

import numpy as np  
import matplotlib.pyplot as plt
import random

# 设置图片大小的原因是默认大小有点看不清
plt.figure(figsize=(12,10))
data=[2,3,3, 2, 1, 4, 4, 4, 4 ,1, 4 ,1, 1 ,3, 3, 1, 4, 1, 4, 4, 7]

ax = plt.subplot(221)
n1,edgeBin1,patches1=plt.hist(data,bins=6,rwidth=0.5)
ax.set_title("bins=6,rwidth=0.3")
print(edgeBin1)			# bins的取值范围:[1. 2. 3. 4. 5. 6. 7.](计算方式data元素中(最大值7-最小值1)/ bins=bins宽度即范围 )
print(n1)				# data中各个元素在bins范围中的个数分别是:[6. 2. 4. 8. 0. 1.]

# 设置label还有color(非)
label = "Label"
colors = "rgmbc"
ax = plt.subplot(222)
n2,edgeBin2,patches2=plt.hist(data,bins=4,rwidth=0.8,label=label)
ax.set_title("bins=4,rwidth=0.8,label = Label")
random.seed()
for patch in patches2:
    patch.set_facecolor(random.choice(colors))
plt.legend()
print(edgeBin2)			# bins的取值范围:[1.  2.5 4.  5.5 7. ]:1-2.5/2.5-4/4-5.5/5.5=7 一共4个区间
print(n2)				# data中各个元素在bins范围中的个数分别是:[8. 4. 8. 1.]

ax = plt.subplot(223)
ax.set_title("bins=7,rwidth=0.3,log=True,cumulative=True")
n,edgeBin,patches = plt.hist(data,bins=7,rwidth=0.3,cumulative=True)
print(edgeBin)			# bins的取值范围:[1.  1.85714286 2.71428571 3.57142857 4.42857143 5.28571429 6.14285714 7.   ]使用(7-1)/7得到bins宽度
print(n)				#data中各个元素在bins范围中的累计个数结果:[ 6.  8. 12. 20. 20. 20. 21.]

bottom = [1, 1, 2, 3,4,5]
ax=plt.subplot(224)
plt.hist(data,bins=6,rwidth=0.5,bottom=bottom)
ax.set_title("bins=6,rwidth=0.5,bottom=bottom")

plt.show()

在这里插入图片描述

  • 展示两个直方图在一个画面中

如果两组数据重合,就会没有办法观看,解决的办法有两种:

  1. 添加alpha参数在.hist()方法中,取值在(0,1)之间
import matplotlib.pyplot as plt
import numpy as np

a = np.random.normal(64, 2, 10000)
b = np.random.normal(loc=70, scale=2, size=10000)

plt.hist(a, range=(55, 75), bins=20, alpha=0.5)
plt.hist(b, range=(55, 75), bins=20, alpha=0.5)
plt.show()

在这里插入图片描述
2. 只画出来直方图的形状,在.hist()方法中,添加参数histtype='step';默认值为"bar",除此之外还有{ ‘barstacked’, ‘step’, ‘stepfilled’}可选,其中“step”就是直方图形状

# 更改上面代码后
plt.hist(a, range=(55, 75), bins=20, histtype='step')
plt.hist(b, range=(55, 75), bins=20, histtype='step')

在这里插入图片描述

  • 两组数据展示出来的直方图一大一小

我们面临的另一个问题是我们的直方图可能有不同数量的样本,使得一个比另一个大得多。结果是两个很难比较的直方图:
在这里插入图片描述
为了解决这个问题,我们可以使用标准化直方图,在.hist()方法中添加参数density=True;如果为真,则返回第一个值是每个区间的百分比,默认是个数

# 注意对比,和上图中的Y轴的刻度,density=True时,展示的是比例
import matplotlib.pyplot as plt
import numpy as np

a = np.random.normal(64, 2, 10000)
b = np.random.normal(loc=70, scale=2, size=100000)

plt.hist(a, range=(55, 75), bins=20, alpha=0.5, density=True)
plt.hist(b, range=(55, 75), bins=20, alpha=0.5, density=True)
plt.show()

在这里插入图片描述

  • 9
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值