matplotlib:boxplot 对象中的传单设置不正确

p-r*_*bot 4 python matplotlib boxplot python-2.7

我正在努力将箱线图中的传单标记更改为我选择的自定义颜色。在前三个值之后,它将恢复为默认值。我看到有几个与此相关的 matplotlib 问题,有什么解决方案吗?

在此先感谢您的帮助!

import matplotlib.pyplot as plt

x = [0.15, 0.11, 0.06, 0.06, 0.12, 0.56]
y = [x, x, x, x, x, x]
boxes = plt.boxplot(y, sym="o")

cols = ['green', 'red', 'blue', 'orange', 'purple', 'black']

for f, fc in zip(boxes['fliers'], cols):
    f.set_color(fc)
    f.set_markersize(40)
    f.set_alpha(0.6)
    f.set_markeredgecolor("None")
    f.set_marker('.')

plt.show()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

Gre*_*reg 5

原始问题中给出的代码在 matplotlib 的开发版本中不起作用v1.5dev。这是因为该set_color方法不会作用于 facecolor,而是应该作用于set_markerfacecolor. 一个完整的工作示例是:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

x = [0.15, 0.11, 0.06, 0.06, 0.12, 0.56]
y = [x, x, x, x, x, x]
boxes = plt.boxplot(y, 
                    flierprops={'alpha':0.6, 
                                'markersize': 40,
                                'markeredgecolor': 'None',
                                'marker': '.'
                                })

cols = ['green', 'red', 'blue', 'orange', 'purple', 'black']

for f, fc in zip(boxes['fliers'], cols):
    f.set_markerfacecolor(fc)

plt.show()
Run Code Online (Sandbox Code Playgroud)

我还移动了所有要设置的固定属性以flierprops提高可读性。