standardizing x-axes in hist

I am trying to produce a series of histograms of

    > related data, for which I want the ranges and scales of
    > the x-axes to be the same. However, I dont see an
    > obvious way of doing this with hist, since specifying
    > nbins will not guarantee the same axis for each. Is
    > there some sort of hack that I can use to do this (or
    > an argument I am overlooking?).

You can force axes in the same figure to share the same xaxes

ax1 = subplot(211)
ax2 = subplot(212, sharex=ax1)

then any changes (panning and zooming) in one will be instantaneously
reflected in the other.

For axes in different figures, you can set the xlim manually

ax1.set_xlim(xmin, xmax)
ax2.set_xlim(xmin, xmax)

There is more sophisticated stuff you can do with observers and event
handling to couple axes between figures in the presence of panning and
zooming if need be.

JDH

Thanks John,

I think I almost have it, but not quite. Say the first dataset is in the range 2-4, and the second is in the range 1-3. At the moment, I am doing as you say, and what happens is that the x axis gets defined by the first plot, and given to the second with the sharex argument. However, this ends up giving the second plot a range of 2-4, so much of the data (from 1-2) is not shown. What I need is for the axes of _both_ plots to be expanded to the range 1-4.

C.

···

On Wednesday, November 01, 2006, at 02:17PM, "John Hunter" <jdhunter@...4...> wrote:

   > I am trying to produce a series of histograms of
   > related data, for which I want the ranges and scales of
   > the x-axes to be the same. However, I dont see an
   > obvious way of doing this with hist, since specifying
   > nbins will not guarantee the same axis for each. Is
   > there some sort of hack that I can use to do this (or
   > an argument I am overlooking?).

You can force axes in the same figure to share the same xaxes

ax1 = subplot(211)
ax2 = subplot(212, sharex=ax1)

then any changes (panning and zooming) in one will be instantaneously
reflected in the other.

For axes in different figures, you can set the xlim manually

ax1.set_xlim(xmin, xmax)
ax2.set_xlim(xmin, xmax)

Once the axes are the same, can one get the actual bars to align? hist() arranges them to look well in their original ranges, so they don't line up together, AFAICT:

#plotting barcharts w/different ranges on same axis
import pylab
a = [1]*2 + [2]*3 + [3]*4
b = [3]*1 + [4]*2 + [5]*3
allim = (min(min(a),min(b)), max(max(a),max(b)))

top = pylab.subplot(211)
vala, bina, patcha = pylab.hist(a)
#top.set_xlim(allim) #doesn't work here
bot = pylab.subplot(212, sharex=top)
valb, binb, patchb = pylab.hist(b)
top.set_xlim(allim)
pylab.show()

I was able to get a common x-axis for all plots, but the bar width remains inconsistent with very thick plots on some histograms, and very thin ones on others. What is the secret here? I need to get equal-width, non-overlapping bars on all the plots.

I'm surprised that these issues have not come up before. For publication in scientific journals, most editors would want consistent scales for any series of plots.

Thanks for any ideas,
Chris

···

On Nov 1, 2006, at 2:15 PM, John Hunter wrote:

    > I am trying to produce a series of histograms of
    > related data, for which I want the ranges and scales of
    > the x-axes to be the same. However, I dont see an
    > obvious way of doing this with hist, since specifying
    > nbins will not guarantee the same axis for each. Is
    > there some sort of hack that I can use to do this (or
    > an argument I am overlooking?).

You can force axes in the same figure to share the same xaxes

ax1 = subplot(211)
ax2 = subplot(212, sharex=ax1)

then any changes (panning and zooming) in one will be instantaneously
reflected in the other.

For axes in different figures, you can set the xlim manually

ax1.set_xlim(xmin, xmax)
ax2.set_xlim(xmin, xmax)

There is more sophisticated stuff you can do with observers and event
handling to couple axes between figures in the presence of panning and
zooming if need be.

--
Christopher Fonnesbeck
fonnesbeck@...935...

When building your histograms, define your bins by means of either
'arange' (to set the same bin width) or 'linspace' (to set
equally-spaced bin limits).

Eg, if all your histograms will have an x-axis ranging from 0 to 100,
and you want the data in each of them plotted into 12 equally-spaced
bins, then all you have to do is:

bins = linspace(0, 100, 13) # sets 13 bin limits, ie 12 bins
h1 = hist(data1, bins)
h2 = hist(data2, bins)
...

If what you want is to plot each data set into 10-units-width bins
(within the same 0-to-100 x-axis):

bins = arange(0, 100+10, 10) # sets bin widths
h1 = hist(data1, bins)
h2 = hist(data2, bins)
...

/Antonio

Christopher Fonnesbeck wrote:

···

On Nov 1, 2006, at 2:15 PM, John Hunter wrote:

    > I am trying to produce a series of histograms of
    > related data, for which I want the ranges and scales of
    > the x-axes to be the same. However, I dont see an
    > obvious way of doing this with hist, since specifying
    > nbins will not guarantee the same axis for each. Is
    > there some sort of hack that I can use to do this (or
    > an argument I am overlooking?).

You can force axes in the same figure to share the same xaxes

ax1 = subplot(211)
ax2 = subplot(212, sharex=ax1)

then any changes (panning and zooming) in one will be instantaneously
reflected in the other.

For axes in different figures, you can set the xlim manually

ax1.set_xlim(xmin, xmax)
ax2.set_xlim(xmin, xmax)

There is more sophisticated stuff you can do with observers and event
handling to couple axes between figures in the presence of panning and
zooming if need be.

I was able to get a common x-axis for all plots, but the bar width
remains inconsistent with very thick plots on some histograms, and
very thin ones on others. What is the secret here? I need to get
equal-width, non-overlapping bars on all the plots.

I'm surprised that these issues have not come up before. For
publication in scientific journals, most editors would want
consistent scales for any series of plots.

Thanks for any ideas,
Chris

--
Christopher Fonnesbeck
fonnesbeck@...935...

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options