Evan,
OK, I see what the problem is: you need to tell the second contour
command to use the same contour levels as the first one is using.
Something like this:
figure(1)
CS1 = contourf(x)
clim(cmin, cmax)
colorbar()
figure(2)
CS2 = contourf(y, levels=CS1.levels)
clim(cmin, cmax)
colorbar()
Does this give the desired result?
The big difference between pcolor and contourf in this context is that
pcolor is automatically generating a colormap with 256 colors, and may
use any or all of those colors, so the colorbar is effectively
continuous. Contourf works with a small discrete set of colors taken
from such a colormap, so colorbar shows the same discrete set. The
boundaries are either generated automatically in contourf, or set via
the levels kwarg. Colorbar gets the boundaries from the output of the
contourf call, which pylab saves and passes along to colorbar even if
you don’t do so explicitly yourself. But pylab has no way of knowing
that you want the contour levels in the second figure to match the
first, so you have to be explicit.
Usually at this point someone chimes in to point out the virtues of
being explicit in your program even when you don’t have to…
Eric
Evan Mason wrote:
Hi Eric, the following lines below will show this. Interestingly, the
correct (or, at least, what I want) behaviour results from using pcolor,
but not with contourf.
a = arange(12, 27, .5)
b = arange(17, 23, .5)
x, y = meshgrid(a, b)
get max and min for clim
cmin = min(x.min(), y.min()) # cmin = 12
cmax = max(x.max(), y.max()) # cmax = 26.5
figure(1)
contourf(x)
#pcolor(x)
clim([cmin, cmax])
colorbar()
figure(2)
contourf(y)
#pcolor(y)
clim([cmin, cmax])
colorbar()
-Evan
On 3/27/07, Eric Firing < efiring@…202… > > <mailto: > efiring@…202…>> wrote:
Evan,
That is getting clearer, but it still seems to me that no override
should be needed in this case; it should "just work", so if it doesn't,
either there is a bug in mpl or a bug in the code. It would be most
helpful if you would write a stripped-down simplest-possible script
that
illustrates the problem. Then I can either show how to fix the script,
or I can use the script to help me track down the mpl bug, if that is
the problem.
Eric
Evan Mason wrote:
> Hi Eric
>
> I am using matplotlib-0.90.0.
>
> I am making 2 contourf subplots of temperature values which have
similar
> but not equal ranges. In subplot1 the range is 15-25; in
subplot2 it is
> 16 to 24. I use clim, giving it the max and min values obtained
from a
> comparison of subplot1 and 2; i.e., I use clim([15, 25]) on both
> subplots. The two subplots display exactly as I want them to.
>
> Next, I want 2 colorbars to go with the two subplots. I want the
range
> of each colorbar to be the same, set as the clim values (15 and 25).
> This happens for subplot1, but not for subplot2, which shows the
range
> 16-24, following its input data range and not the clim values.
>
> So what I want to know is if it is possible to overide the colorbar
> limits, setting them to 15-25?
>
> I hope that's clearer now, thanks, Evan
>
>
>
> On 3/27/07, * Eric Firing* < > efiring@...202... > > <mailto:efiring@...202...> > > > <mailto:efiring@...202... <mailto: > efiring@...202...>>> wrote:
>
> Evan,
>
> It is still not quite clear to me what you want versus what
you are
> getting. With recent versions of mpl, the colorbar
automatically uses
> the same color boundaries as contourf, if that is what the
colorbar is
> tracking. What version of mpl are you using?
>
> (With the most recent mpl I see that there is a strange bug
such that
> setting clim clobbers the axis tick labeling for the
colorbar--yet
> another thing that needs to be figured out and fixed.)
>
>