Automatic resizing of colorbar

Hi,

I would like to plot a colorbar which automatically gets resized when I change the view limits and the aspect ratio of the main axes. So for example:

import matplotlib.pyplot as mpl
import numpy as np

fig = mpl.figure()
ax = fig.add_axes([0.1,0.1,0.7,0.8])
cax = fig.add_axes([0.81,0.1,0.02,0.8])

image = ax.imshow(np.random.random((100,100)))

fig.colorbar(image, cax=cax)

Is fine, but then if I interactively select a sub-region to zoom in with a different aspect ratio, which I can also emulate by doing

ax.set_ylim(40.,60.)

The colorbar is then too high. If I then do

ax.set_xlim(50.,55.)

The height is fine but the position would need changing.

Is there an easy way to get around this issue and have the colorbar always at a fixed distance from the main axes, and also have it resize? Or is the only way to write this all explicitly using event callbacks?

Thanks for any help,

Thomas

Yes, axes location in mpl, by design, is specified in normalized
figure coordinate.
And, for the colorbar axes to match the height (or width) of the
parent axes always , you need to manually update the location of the
colorbar axes.
There are a few ways to do this. You may use event callbacks, use
custom axes class, or use Axes._axes_locator attribute (which is a
callable object that returns the new axes postion).

The axes_grid toolkit has some helper functions for this, and you may
take a look if interested.

http://matplotlib.sourceforge.net/examples/axes_grid/demo_axes_divider.html

-JJ

ยทยทยท

On Mon, Dec 14, 2009 at 2:18 PM, Thomas Robitaille <thomas.robitaille@...287...> wrote:

Hi,

I would like to plot a colorbar which automatically gets resized when
I change the view limits and the aspect ratio of the main axes. So for
example:

import matplotlib.pyplot as mpl
import numpy as np

fig = mpl.figure()
ax = fig.add_axes([0.1,0.1,0.7,0.8])
cax = fig.add_axes([0.81,0.1,0.02,0.8])

image = ax.imshow(np.random.random((100,100)))

fig.colorbar(image, cax=cax)

Is fine, but then if I interactively select a sub-region to zoom in
with a different aspect ratio, which I can also emulate by doing

ax.set_ylim(40.,60.)

The colorbar is then too high. If I then do

ax.set_xlim(50.,55.)

The height is fine but the position would need changing.

Is there an easy way to get around this issue and have the colorbar
always at a fixed distance from the main axes, and also have it
resize? Or is the only way to write this all explicitly using event
callbacks?

Thanks for any help,

Thomas

------------------------------------------------------------------------------
Return on Information:
Google Enterprise Search pays you back
Get the facts.
http://p.sf.net/sfu/google-dev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users