facecolor kwarg for Matplotlib stacked histogram

*爱你&永不变心* 提交于 2019-12-07 06:19:50

问题


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']
    )

it throws the following error:

ValueError: to_rgba: Invalid rgba arg "['lightblue', 'lightgreen', 'crimson']"
could not convert string to float: lightblue

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.


回答1:


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')


来源:https://stackoverflow.com/questions/17512730/facecolor-kwarg-for-matplotlib-stacked-histogram

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!