facepour kwarg for Matplotlib堆积直方图

xvt*_*vtk 5 matplotlib histogram python-2.7

我无法控制使用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'
    )
Run Code Online (Sandbox Code Playgroud)

但是,当我引入额外的直方图时,

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']
    )
Run Code Online (Sandbox Code Playgroud)

它会引发以下错误:

ValueError: to_rgba: Invalid rgba arg "['lightblue', 'lightgreen', 'crimson']"
could not convert string to float: lightblue
Run Code Online (Sandbox Code Playgroud)

使用该color=['lightblue', 'lightgreen', 'crimson']选项确实有效,但我希望能够直接控制填充和线条颜色,同时能够使用指定的Matplotlib颜色.我使用的是Matplotlib 1.2.1版.

小智 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')
Run Code Online (Sandbox Code Playgroud)