Matplotlib - 统计图

二维网格图

在此演示如何用 Matplotlib ( python 第三方库 ) 绘制一些常见的统计图.

hist() 绘制 直方图 ( histogram ).

示例

效果图

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import matplotlib.pyplot as plt
import numpy as np

data1 = np.random.normal(3, 1, 10000)
data2 = np.random.normal(6, 1, 10000)
data3 = np.random.normal(9, 1, 10000)

fig1 = plt.figure()
plt.hist(data1, bins=20, histtype='stepfilled', alpha=0.5, color='#ff165d', edgecolor='#ff9999', label='l1')
plt.hist(data2, bins=20, histtype='stepfilled', alpha=0.5, color='#ff9a00', edgecolor='#ffaaa5', label='l2')
plt.hist(data3, bins=20, histtype='stepfilled', alpha=0.5, color='#3ec1d3', edgecolor='#a5dee5', label='l3')

plt.title('histogram 1')
plt.legend()
plt.show()

bins 沿着 x 轴划分多少个独立的绘图区域
histtype 绘图类型 ( barbarstackedstepstepfilled )
alpha 透明度
edgecolor 边框颜色
label 右上角 标签