Python数据可视化第 7 讲:matplotlib绘制直方图函数hist
发布时间
阅读量:
阅读量
1. hist 函数介绍
该函数负责生成频数分布图;这种图表的本质是一种统计图表。该函数绘制的数据来源于参数 x;其中参数 x 包含多个数据值,并可表示为长度多样的数据集合列表(例如[x₀, x₁,…]),或可被视为每个列对应独立数据集的二维数组结构。函数的具体调用格式如下所示:
hist(x, bins, **kwargs)
hist(x)
AI助手
主要参数说明:
x:输入值为数组或序列类型的数据,默认情况下会自动转换为一维数组;该函数支持接收单个数组或多个不同长度的数组作为输入数据源。
bins:用于指定直方图中的箱体划分方式,默认采用等宽区间划分;具体来说:
- 当 bins 为整数值时,则表示在数据范围区间内等分成若干个宽度相同的箱体;
- 若 bins 为一个有序序列,则该序列元素将作为各个箱子之间的分隔线;
- 当 bins 为字符串形式时,则将被解释为不同的分位点分割方式;
特别需要注意的是,在使用非整数类型的 bins 参数时,默认会将最后一个区间设置为闭区间形式;
举个例子:当 bins 设定为 [1, 2, 3, 4] 时,
第一个箱体覆盖区间 [1,2),第二个箱体覆盖区间 [2,3),而最后一个箱体则覆盖 [3,4] 的闭区间范围。
2. hist 函数绘图示例
2.1 绘制一个简单的直方图
生成一个简单的频率分布直方图。代码如下所示:包含绘图所需的数据的数据集x被生成一个频率分布直方图所需的图形数据,并设置分组数目为6个区间。这表示将数据集x划分为6个相邻区间,并对每个区间中的数据进行计数统计。具体来说,在区间[1,1.5)、[1.5,2.0)、[2.0,2.5)、...、[3.5,4]内分别计算数据出现的次数。频率分布直方图中每个区间的高度数值即代表了对应范围内出现的数据点数量
import matplotlib.pyplot as plt
# step1:准备画图的数据
x = [1, 2, 3, 4, 1, 2, 3, 4, 3, 2, 4, 2, 1, 4, 3, 2, 2, 2, 2, 3, 3, 2, 3, 4, 2, 2, 2]
# step2:手动创建一个figure对象,相当于一个空白的画布
figure = plt.figure()
# step3:在画布上添加1个子块,标定绘图位置
axes1 = plt.subplot(1, 1, 1)
# step4:绘制直方图
axes1.hist(x, bins=6)
axes1.set_title('a simple histogram')
# step5:展示
plt.show()
AI助手
上面代码的运行结果:很明显,落在区间[2.0,2.5) 的数据是最多的。

2.2 通过numpy生成数据绘制一个直方图
通过 numpy 生成数据绘制一个直方图,代码如下:
import matplotlib.pyplot as plt
import numpy as np
# step1:准备画图的数据
N_points = 100000
n_bins = 20
x = np.random.randn(N_points)
# step2:手动创建一个figure对象,相当于一个空白的画布
figure = plt.figure()
plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题
# step3:在画布上添加1个子块,标定绘图位置
axes1 = plt.subplot(1, 1, 1)
# step4:绘制直方图
axes1.hist(x, bins=n_bins)
axes1.set_title('a simple histogram')
# step5:展示
plt.show()
AI助手
上面代码的运行结果:

2.3 绘制具有多个数据集的直方图
绘制具有多个数据集的直方图,并设置基本属性,代码示例如下:
import matplotlib.pyplot as plt
import numpy as np
# step1:准备画图的数据
np.random.seed(19680801)
n_bins = 10
x = np.random.randn(1000, 3)
colors = ['red', 'tan', 'lime']
# step2:手动创建一个figure对象,相当于一个空白的画布
figure = plt.figure()
plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题
# step3:在画布上添加1个子块,标定绘图位置
axes1 = plt.subplot(1, 1, 1)
# step4:绘制直方图
axes1.hist(x, n_bins, density=True, histtype='bar', color=colors, label=colors)
axes1.set_title('a simple histogram')
axes1.legend()
# step5:展示
plt.show()
AI助手
上面代码的运行结果:

2.4 直方图与拟合曲线
在绘制直方图的基础上,绘制一条拟合曲线,代码如下:
import matplotlib.pyplot as plt
import numpy as np
# step1:准备画图的数据
mu = 100 # mean of distribution
sigma = 15 # standard deviation of distribution
x = mu + sigma * np.random.randn(437)
num_bins = 50
# step2:手动创建一个figure对象,相当于一个空白的画布
figure = plt.figure()
plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题
# step3:在画布上添加1个子块,标定绘图位置
axes1 = plt.subplot(1, 1, 1)
# step4:绘制直方图
n, bins, patches = axes1.hist(x, num_bins, density=1)
# step5:绘制一条拟合曲线
y = ((1 / (np.sqrt(2 * np.pi) * sigma)) *
np.exp(-0.5 * (1 / sigma * (bins - mu)) ** 2))
axes1.plot(bins, y, '--')
# step6:设置基本元素
axes1.set_xlabel('Smarts')
axes1.set_ylabel('Probability density')
axes1.set_title(r'Histogram of IQ: $\mu=100$, $\sigma=15$')
# step7:展示
plt.show()
AI助手
上面代码的运行结果:

全部评论 (0)
还没有任何评论哟~
