question about stacked histograms

Hi all,

Has any of you had any luck with creating stacked histograms using matplotlib? It seems to work but I have no idea how to label (or add the legend) or choose the colors of the stacks. Below is a sample code for creating a stacked histogram. Can anyone help please? Unlike the "bar()" function, hist() doesn't seem to have the color/colors parameter.

#!/usr/bin/python

import sys
import matplotlib.pyplot as pyplot
import numpy as numpy

page_numbers_one = (100,100,500,600,800)
page_numbers_two = (100,100,500,600,800,100,100,100,100,100)
page_numbers_three = (900,100,500,600,800,500)

pyplot.hist((page_numbers_one,page_numbers_two,page_numbers_three),histtype='barstacked',bins=5)

pyplot.show()

best,
amit shrestha

Have you seen this example:

  http://matplotlib.sourceforge.net/examples/pylab_examples/bar_stacked.html

To create the legend, it is best to create proxy objects, eg
Rectangles, with the right colors and manually add them to the legend.
See

  http://matplotlib.sourceforge.net/users/plotting/legend.html#using-proxy-artist

JDH

···

On Tue, Jun 2, 2009 at 3:15 PM, Amit <rorokimdim@...287...> wrote:

Hi all,

Has any of you had any luck with creating stacked histograms using
matplotlib? It seems to work but I have no idea how to label (or add the
legend) or choose the colors of the stacks. Below is a sample code for
creating a stacked histogram. Can anyone help please? Unlike the "bar()"
function, hist() doesn't seem to have the color/colors parameter.

#!/usr/bin/python

import sys
import matplotlib.pyplot as pyplot
import numpy as numpy

page_numbers_one = (100,100,500,600,800)
page_numbers_two = (100,100,500,600,800,100,100,100,100,100)
page_numbers_three = (900,100,500,600,800,500)

pyplot.hist((page_numbers_one,page_numbers_two,page_numbers_three),histtype='barstacked',bins=5)

pyplot.show()


Thanks John. The bar_stacked example does not seem to work for histograms, but I'm glad to know that we could manually make the legends. I still do not know how to specify the colors of the "stacks" in my histogram, so adding the manual legends for them is still a pain -- but at least doable since I can figure it out from the actual data.
best,
amit shrestha

John Hunter wrote:

···

<rorokimdim@…287…>http://matplotlib.sourceforge.net/examples/pylab_examples/bar_stacked.htmlhttp://matplotlib.sourceforge.net/users/plotting/legend.html#using-proxy-artist