盒形图中箱子的面板图案

9
我想要做类似于以下这个例子(使用matplotlib):enter image description here (来自Colorfill boxplot in R-cran with lines, dots, or similar
我看到一些有关填充的信息,但我真的不知道如何使用它。
此外,我想知道如何更改参数,例如boxprops dict的可能属性 - 在plt.boxplot(...,boxprops = boxpropsdict)中使用。是否可以只列出所有可能的属性列表?
1个回答

18
重要的一点是在调用 `boxplot` 函数时设置 `patch_artist=True`。
import numpy as np
import matplotlib.pyplot as plt

# fake up some data
spread= np.random.rand(50) * 100
center = np.ones(25) * 50
flier_high = np.random.rand(10) * 100 + 100
flier_low = np.random.rand(10) * -100
data = np.concatenate((spread, center, flier_high, flier_low), 0)

# basic plot
bp = plt.boxplot(data, patch_artist=True)

for box in bp['boxes']:
    # change outline color
    box.set(color='red', linewidth=2)
    # change fill color
    box.set(facecolor = 'green' )
    # change hatch
    box.set(hatch = '/')

plt.show()

基本的绘图示例取自箱线图演示。然而,这些示例中没有一个设置patch_artist=True。如果省略该语句,将会出现以下错误:

AttributeError: 'Line2D' object has no attribute 'set_facecolor'

箱线图演示2详细展示了如何将矩形适配到箱线图以获得着色效果。这篇博客指向了patch_artist选项。
有关阴影的更多想法,请参考阴影演示。上面的示例生成了这个图:

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接