Several histograms on one set of axes?

Hi all, maybe this is a daft question, but is there a

    > simple way of drawing several histograms on top of one
    > another on the same axes?

    > Right now I can't even figure out how to change the bar
    > colour in a script. I've looked through the code in axes.py
    > and this looks like it *ought* to be right, but isn't:

    > ax1 = pylab.axes(...) n, bins, patches = ax1.hist(data,
    > color='r')

    > Any ideas would be very much appreciated ...

Ahh yes, the docs could be a little clearer. They say

        kwargs are used to update the properties of the
        hist bars

To make sense of this you would need to know that the bars are
matplotlib.patch.Rectangle objects, and know what properties could be
set on them. We have a goal of making the documentation thorough with
respect to kwargs. In the meantime, scroll through the rectangle
class documentation at
http://matplotlib.sf.net/matplotlib.patches.html for insight.

Here is a hint about how to find these kinds of things out yourself:
Fire up an interactive python shell with support for matplotlib (see
http://matplotlib.sf.net/interactive.html). I use ipython
(http://ipython.scipy.org) in pylab mode. Make a histogram and use
the setp functionality to inspect the properties of the patches
returned by hist. patches are 2D objects like polygons, circles and
rectangles.

johnh@...1066...:~> ipython -pylab
/home/titan/johnh/local/lib/python2.3/site-packages/matplotlib/__init__.py:892:
Python 2.3.4 (#12, Jul 2 2004, 09:48:10)
Type "copyright", "credits" or "license" for more information.
IPython 0.7.2.svn -- An enhanced Interactive Python.
? -> Introduction to IPython's features.
%magic -> Information about IPython's 'magic' % functions.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.

  Welcome to pylab, a matplotlib-based Python environment.
  For more information, type 'help(pylab)'.

In [1]: n,bins,patches = hist(randn(1000), 20)

In [2]: setp(patches)
    alpha: float
    animated: [True | False]
    antialiased or aa: [True | False]
    bounds: (left, bottom, width, height)
    clip_box: a matplotlib.transform.Bbox instance
    clip_on: [True | False]
    edgecolor or ec: any matplotlib color - see help(colors)
    facecolor or fc: any matplotlib color - see help(colors)
    figure: a matplotlib.figure.Figure instance
    fill: [True | False]
    hatch: unknown
    height: float
    label: any string
    linewidth or lw: float
    lod: [True | False]
    transform: a matplotlib.transform transformation instance
    visible: [True | False]
    width: float
    x: float
    y: float
    zorder: any number

Scrolling through this list, you see things like edgecolor and
facecolor. You can pass these as kwargs to hist, or use setp

In [3]: setp(patches, edgecolor='g', facecolor='b', linewidth=2);

Hope this helps,
JDH

here is a code snippet that i hacked in in order to do this - two
histograms on top of each other, like in the attached graph…
hopefully it can give you some ideas.

n, bins, patches = pylab.hist(engine.hist_normal, [i for i in pylab.arange(0, 1, bin_width)])

n1, bins, patches1 = pylab.hist(engine.hist_pathological, [i for i in pylab.arange(0, 1, bin_width)])

for p in patches:

p.set_facecolor(‘g’)

p.width = p.width / 2

for p in patches1:

p.width = p.width / 2

p.xy = [p.xy[0] + (bin_width/2), p.xy[1]]

lisa

IPC-512-eq-128-MPA-512-eq-128-3-app.jpg

···

On 3/30/06, John Hunter <jdhunter@…8…> wrote:

> Hi all, maybe this is a daft question, but is there a
> simple way of drawing several histograms on top of one

> another on the same axes?

> Right now I can't even figure out how to change the bar
> colour in a script. I've looked through the code in axes.py
> and this looks like it *ought* to be right, but isn't:


> ax1 = pylab.axes(...)  n, bins, patches = ax1.hist(data,
> color='r')

> Any ideas would be very much appreciated ...

Ahh yes, the docs could be a little clearer. They say

    kwargs are used to update the properties of the
    hist bars

To make sense of this you would need to know that the bars are
matplotlib.patch.Rectangle objects, and know what properties could be

set on them. We have a goal of making the documentation thorough with
respect to kwargs. In the meantime, scroll through the rectangle
class documentation at

http://matplotlib.sf.net/matplotlib.patches.html
for insight.

Here is a hint about how to find these kinds of things out yourself:
Fire up an interactive python shell with support for matplotlib (see

http://matplotlib.sf.net/interactive.html
). I use ipython
(http://ipython.scipy.org) in pylab mode. Make a histogram and use
the setp functionality to inspect the properties of the patches

returned by hist. patches are 2D objects like polygons, circles and
rectangles.

johnh@…1066…:~> ipython -pylab
/home/titan/johnh/local/lib/python2.3/site-packages/matplotlib/init.py:892:
Python 2.3.4 (#12, Jul 2 2004, 09:48:10)
Type “copyright”, “credits” or “license” for more information.
IPython 0.7.2.svn – An enhanced Interactive Python.
? -> Introduction to IPython’s features.

%magic -> Information about IPython’s ‘magic’ % functions.
help -> Python’s own help system.
object? -> Details about ‘object’. ?object also works, ?? prints more.

Welcome to pylab, a matplotlib-based Python environment.

For more information, type ‘help(pylab)’.

In [1]: n,bins,patches = hist(randn(1000), 20)

In [2]: setp(patches)
alpha: float
animated: [True | False]
antialiased or aa: [True | False]

bounds: (left, bottom, width, height)
clip_box: a matplotlib.transform.Bbox instance
clip_on: [True | False]
edgecolor or ec: any matplotlib color - see help(colors)
facecolor or fc: any matplotlib color - see help(colors)

figure: a matplotlib.figure.Figure instance
fill: [True | False]
hatch: unknown
height: float
label: any string
linewidth or lw: float
lod: [True | False]
transform: a matplotlib.transform transformation instance
visible: [True | False]
width: float
x: float
y: float
zorder: any number

Scrolling through this list, you see things like edgecolor and

facecolor. You can pass these as kwargs to hist, or use setp

In [3]: setp(patches, edgecolor=‘g’, facecolor=‘b’, linewidth=2);

Hope this helps,
JDH


This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!

http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642


Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users