Matplotlib堆叠直方图的facecolor kwarg
我无法控制使用Matplotlib的hist
函数和stacked=True
绘制的直方图的颜色和线条样式.对于单个非堆叠直方图,我没有问题:
I am having trouble controlling the color and linestyle of histogram plotted using Matplotlib's hist
function with stacked=True
. For a single non-stacked histogram, I have no trouble:
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'
)
但是,当我引入其他直方图时,
However, when I introduce additional histograms,
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版本.
Using the color=['lightblue', 'lightgreen', 'crimson']
option does work, but I would like to have direct control of the fill and line colors separately while being able to use the named Matplotlib colors. I am using version 1.2.1 of Matplotlib.
facecolor
必须是单个命名的颜色,而不是列表,但要添加此颜色
使用P.hist
后可能会为您完成工作:
facecolor
needs to be a single named color, not a list, but adding this
after your P.hist
usage might get the job done for you:
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')