我无法控制使用Matplotlib的hist
函数和stacked=True
绘制的直方图的颜色和线条样式。对于单个非堆叠直方图,我没有遇到麻烦:
import pylab as P
mu, sigma = 200, 25
x0 = mu + sigma*P.randn(10000)
n, bins, patches = P.hist(
x0, 20,
histtype='stepfilled',
facecolor='lightblue'
)
然而,当我引入额外的直方图时,
import pylab as P
mu, sigma = 200, 25
x0 = mu + sigma*P.randn(10000)
x1 = mu + sigma*P.randn(7000)
x2 = mu + sigma*P.randn(3000)
n, bins, patches = P.hist(
[x0,x1,x2], 20,
histtype='stepfilled',
stacked=True,
facecolor=['lightblue','lightgreen','crimson']
)
它会抛出以下错误:
ValueError: to_rgba: Invalid rgba arg "['lightblue', 'lightgreen', 'crimson']"
could not convert string to float: lightblue
使用color=['lightblue', 'lightgreen', 'crimson']
选项确实有效,但我希望能够直接控制填充和线条颜色,同时能够使用指定的Matplotlib颜色。我使用的是Matplotlib版本1.2.1。
答案 0 :(得分:3)
facecolor
需要是单个命名颜色,而不是列表,而是添加此颜色
在P.hist
使用后,您可以完成工作:
for patch in patches[0]: patch.set_facecolor('lightblue')
for patch in patches[1]: patch.set_facecolor('lightgreen')
for patch in patches[2]: patch.set_facecolor('crimson')