manual placement of a colorbar

Hi

I have a Basemap and I want to manually add a colorbar to the side. As you can see in the attached image, the colorbar is not correctly positioned.

Here is a snippet of code

ax=fig.add_axes([0.1,0.1,0.8,.8])
m = Basemap(resolution=‘c’,projection=‘cyl’,lon_0=0,ax=ax)

cax = fig.add_axes([0.9, 0.1, 0.1, 0.8]) # setup colorbar axes.
cmap = mpl.cm.cool
norm = mpl.colors.Normalize(vmin=5, vmax=10)
cb1 = mpl.colorbar.ColorbarBase(cax, cmap=cmap,
norm=norm,
orientation=‘vertical’)

what am I doing wrong?

Mathew

image.png

Nothing. You just have to tweak the cax parameters to get it positioned where you want it.

A quick and dirty approach would be to let matplotlib position the colorbar by using

import matplotlib.pyplot as plt

<plot stuff on basemap>

plt.colorbar(orientation='vertical',shrink=0.5) # play with the shrink param to get it to be the same size as the map

-Jeff

···

On 4/1/10 1:24 PM, Mathew Yeates wrote:

Hi

I have a Basemap and I want to manually add a colorbar to the side. As you can see in the attached image, the colorbar is not correctly positioned.

Here is a snippet of code

ax=fig.add_axes([0.1,0.1,0.8,.8])
m = Basemap(resolution='c',projection='cyl',lon_0=0,ax=ax)

cax = fig.add_axes([0.9, 0.1, 0.1, 0.8]) # setup colorbar axes.
cmap = mpl.cm.cool
norm = mpl.colors.Normalize(vmin=5, vmax=10)
cb1 = mpl.colorbar.ColorbarBase(cax, cmap=cmap,
                                       norm=norm,
                                       orientation='vertical')

what am I doing wrong?

Mathew

--
Jeffrey S. Whitaker Phone : (303)497-6313
Meteorologist FAX : (303)497-6449
NOAA/OAR/PSD R/PSD1 Email : Jeffrey.S.Whitaker@...259...
325 Broadway Office : Skaggs Research Cntr 1D-113
Boulder, CO, USA 80303-3328 Web : http://tinyurl.com/5telg

This doesn’t work for what I’m doing. Unless I call “contour” or something similar, this will fail. I am only using m.plot and this doesn’t set things up properly to call “colorbar”.

···

On Thu, Apr 1, 2010 at 12:40 PM, Jeff Whitaker <jswhit@…146…> wrote:

On 4/1/10 1:24 PM, Mathew Yeates wrote:

Hi

I have a Basemap and I want to manually add a colorbar to the side. As you can see in the attached image, the colorbar is not correctly positioned.

Here is a snippet of code

ax=fig.add_axes([0.1,0.1,0.8,.8])

m = Basemap(resolution=‘c’,projection=‘cyl’,lon_0=0,ax=ax)

cax = fig.add_axes([0.9, 0.1, 0.1, 0.8]) # setup colorbar axes.

cmap = mpl.cm.cool

norm = mpl.colors.Normalize(vmin=5, vmax=10)

cb1 = mpl.colorbar.ColorbarBase(cax, cmap=cmap,

                                   norm=norm,

                                   orientation='vertical')

what am I doing wrong?

Mathew

Nothing. You just have to tweak the cax parameters to get it positioned where you want it.

A quick and dirty approach would be to let matplotlib position the colorbar by using

import matplotlib.pyplot as plt

plt.colorbar(orientation=‘vertical’,shrink=0.5) # play with the shrink param to get it to be the same size as the map

-Jeff

Jeffrey S. Whitaker Phone : (303)497-6313

Meteorologist FAX : (303)497-6449

NOAA/OAR/PSD R/PSD1 Email : Jeffrey.S.Whitaker@…259…

325 Broadway Office : Skaggs Research Cntr 1D-113

Boulder, CO, USA 80303-3328 Web : http://tinyurl.com/5telg

If you're not afraid of contaminating your code with axes_grid toolkit,

instead

cax = fig.add_axes([0.9, 0.1, 0.1, 0.8]) # setup colorbar axes.

try

from mpl_toolkits.axes_grid import make_axes_locatable
import matplotlib.axes as maxes

divider = make_axes_locatable(ax)
cax = divider.new_horizontal("5%", pad=0.05, axes_class=maxes.Axes)
fig.add_axes(cax)

This way, the height of the colorbar always matches that of your main axes.

Regards,

-JJ

···

On Thu, Apr 1, 2010 at 3:24 PM, Mathew Yeates <mat.yeates@...287...> wrote:

Hi

I have a Basemap and I want to manually add a colorbar to the side. As you
can see in the attached image, the colorbar is not correctly positioned.

Here is a snippet of code

ax=fig.add_axes([0.1,0.1,0.8,.8])
m = Basemap(resolution='c',projection='cyl',lon_0=0,ax=ax)

cax = fig.add_axes([0.9, 0.1, 0.1, 0.8]) # setup colorbar axes.
cmap = mpl.cm.cool
norm = mpl.colors.Normalize(vmin=5, vmax=10)
cb1 = mpl.colorbar.ColorbarBase(cax, cmap=cmap,
norm=norm,
orientation='vertical')

what am I doing wrong?

Mathew

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Thanks a bunch JJ. I’ve been trying to figure that one out all day!

Mathew

···

On Thu, Apr 1, 2010 at 1:04 PM, Jae-Joon Lee <lee.j.joon@…287…> wrote:

If you’re not afraid of contaminating your code with axes_grid toolkit,

instead

cax = fig.add_axes([0.9, 0.1, 0.1, 0.8]) # setup colorbar axes.

try

from mpl_toolkits.axes_grid import make_axes_locatable

import matplotlib.axes as maxes

divider = make_axes_locatable(ax)

cax = divider.new_horizontal(“5%”, pad=0.05, axes_class=maxes.Axes)

fig.add_axes(cax)

This way, the height of the colorbar always matches that of your main axes.

Regards,

-JJ

On Thu, Apr 1, 2010 at 3:24 PM, Mathew Yeates <mat.yeates@…287…> wrote:

Hi

I have a Basemap and I want to manually add a colorbar to the side. As you

can see in the attached image, the colorbar is not correctly positioned.

Here is a snippet of code

ax=fig.add_axes([0.1,0.1,0.8,.8])

m = Basemap(resolution=‘c’,projection=‘cyl’,lon_0=0,ax=ax)

cax = fig.add_axes([0.9, 0.1, 0.1, 0.8]) # setup colorbar axes.

cmap = mpl.cm.cool

norm = mpl.colors.Normalize(vmin=5, vmax=10)

cb1 = mpl.colorbar.ColorbarBase(cax, cmap=cmap,

                                   norm=norm,
                                   orientation='vertical')

what am I doing wrong?

Mathew


Download Intel® Parallel Studio Eval

Try the new software tools for yourself. Speed compiling, find bugs

proactively, and fine-tune applications for parallel performance.

See why Intel Parallel Studio got high marks during beta.

http://p.sf.net/sfu/intel-sw-dev


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users