Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

facecolor kwarg for Matplotlib stacked histogram

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.

like image 837
xvtk Avatar asked Oct 22 '22 06:10

xvtk


1 Answers

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')
like image 117
cloudpoint Avatar answered Oct 27 '22 09:10

cloudpoint