Matplotlib is hiring a Research Software Engineering Fellow! See discourse for details. Apply by January 3, 2020
此图说明如何创建两种类型的方框图(矩形和凹口),以及如何通过访问方框图的艺术家的属性来填充自定义颜色。另外, labels
参数用于为每个样本提供X记号标签。
关于箱线图及其历史的一个很好的一般参考资料可以在这里找到:http://vita.had.co.nz/papers/boxplots.pdf
import matplotlib.pyplot as plt
import numpy as np
# Random test data
np.random.seed(19680801)
all_data = [np.random.normal(0, std, size=100) for std in range(1, 4)]
labels = ['x1', 'x2', 'x3']
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(9, 4))
# rectangular box plot
bplot1 = axes[0].boxplot(all_data,
vert=True, # vertical box alignment
patch_artist=True, # fill with color
labels=labels) # will be used to label x-ticks
axes[0].set_title('Rectangular box plot')
# notch shape box plot
bplot2 = axes[1].boxplot(all_data,
notch=True, # notch shape
vert=True, # vertical box alignment
patch_artist=True, # fill with color
labels=labels) # will be used to label x-ticks
axes[1].set_title('Notched box plot')
# fill with colors
colors = ['pink', 'lightblue', 'lightgreen']
for bplot in (bplot1, bplot2):
for patch, color in zip(bplot['boxes'], colors):
patch.set_facecolor(color)
# adding horizontal grid lines
for ax in axes:
ax.yaxis.grid(True)
ax.set_xlabel('Three separate samples')
ax.set_ylabel('Observed values')
plt.show()