colorbar, setting limits

Hi
I want to set the limits of my colorbar to something different from that of my plot. For example I have a contourf plot where I set the colour limits to be, say:

clim([15, 25])

But I want the limits of the colorbar to be 10 and 30. I suspect this is something to do with mappable and vmin and vmax, but I don’t see anything specific about how to do this, either on this forum or on mpl/sourceforge.

Thanks for any help, Evan

Sorry, I realise that my example in my original post doesn’t quite make sense. My data range is 17 to 23. I set clim to be clim([15, 25]). I then want the colorbar limits to be the same as clim, 15 to 25. The questions remains the same, which is how to set the range for colorbar.

Thanks, Evan

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.)

Eric

Evan Mason wrote:

···

Sorry, I realise that my example in my original post doesn't quite make sense. My data range is 17 to 23. I set clim to be clim([15, 25]). I then want the colorbar limits to be the same as clim, 15 to 25. The questions remains the same, which is how to set the range for colorbar.

Thanks, Evan

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…> 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.)

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…>> 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.)

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…> 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...> 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.)

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…>> 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.)
     >

Thanks, Eric,

Yes, that works for the stripped down version, but not for what I am trying to do. The examples I have given have the range of one of the plots nicely fitting inside that of the other (ie, plot 1 range 15 to 26, plot 2 range 17 to 23), but sometimes with what I am doing I have, for example, plot 1 range 15 to 23 and plot 2 range 17 to 25. In this case, passing the levels from plot 1 to plot 2 means that levels 24 and 25 of plot 2 are stripped away; this, at least, is what I think is happening because I have white areas of my plot that weren’t there before. It seems to me that it would be quite useful to have the option to override the colorbar range, setting it to be the same as defined by clim, or some other values.

As a solution for now, I think I will just use one colorbar for the two plots. Thanks for your help with this, and the tip about being explicit in my programs!

-Evan

···

On 3/27/07, Eric Firing <efiring@…202…> wrote:

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.)
 >
 >