具有自定义填充颜色的箱线图#

为了单独为箱线图的每个箱体着色

  1. 使用关键字参数 patch_artist=True 来创建填充的箱体。

  2. 遍历创建的箱体并调整其颜色。

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(19680801)
fruit_weights = [
    np.random.normal(130, 10, size=100),
    np.random.normal(125, 20, size=100),
    np.random.normal(120, 30, size=100),
]
labels = ['peaches', 'oranges', 'tomatoes']
colors = ['peachpuff', 'orange', 'tomato']

fig, ax = plt.subplots()
ax.set_ylabel('fruit weight (g)')

bplot = ax.boxplot(fruit_weights,
                   patch_artist=True,  # fill with color
                   tick_labels=labels)  # will be used to label x-ticks

# fill with colors
for patch, color in zip(bplot['boxes'], colors):
    patch.set_facecolor(color)

plt.show()
boxplot color

标签:样式:颜色 领域:统计 绘图类型:箱线图

参考

本示例展示了以下函数、方法、类和模块的使用

由 Sphinx-Gallery 生成的画廊