colorbar()

Is there a way to get the colorbar to work with an axes instance.

ax2 = axes([0.2, 0.1, 0.6, 0.8], axisbg=‘w’)

ax2.fill([x1,x2,x2,x1], [y1,y1,y2,y2], fc=‘None’, ec=‘r’)
ax2.pcolormesh(X, Y, newa, shading=‘flat’, cmap=cm.YlOrRd)#gray_r)
ax2.axvline(x=0, color=‘gray’, linestyle=’–’)

ax2.axhline(y=0, color=‘gray’, linestyle=’–’)
ax2.plot([offaxisX], [offaxisY], ‘r+’, mew=1)
ax2.colorbar()

AttributeError: ‘Axes’ object has no attribute ‘colorbar’

colorbar()

, line 499, in
colorbar()
File “C:\Python25\Lib\site-packages\matplotlib\pyplot.py”, line 1129, in colorbar
ret = gcf().colorbar(mappable, cax = cax, ax=ax, **kw)
File “C:\Python25\Lib\site-packages\matplotlib\figure.py”, line 956, in colorbar

cb = cbar.Colorbar(cax, mappable, **kw)

File “C:\Python25\Lib\site-packages\matplotlib\colorbar.py”, line 558, in init
mappable.autoscale_None() # Ensure mappable.norm.vmin, vmax
AttributeError: ‘NoneType’ object has no attribute ‘autoscale_None’

I am not sure how the mappable, ax, and cax options work.

···


“The game of science can accurately be described as a never-ending insult to human intelligence.” - João Magueijo

"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. " -Albert Einstein

Is there a way to get the colorbar to work with an axes instance.

ax2 = axes([0.2, 0.1, 0.6, 0.8], axisbg='w')
ax2.fill([x1,x2,x2,x1], [y1,y1,y2,y2], fc='None', ec='r')
ax2.pcolormesh(X, Y, newa, shading='flat', cmap=cm.YlOrRd)#gray_r)
ax2.axvline(x=0, color='gray', linestyle='--')
ax2.axhline(y=0, color='gray', linestyle='--')
ax2.plot([offaxisX], [offaxisY], 'r+', mew=1)
ax2.colorbar()

AttributeError: 'Axes' object has no attribute 'colorbar'

I think you are looking for the Figure.colorbar() method if you are using the
OO API. If you are using the pylab interface, it is pylab.colorbar:

colorbar()

, line 499, in <module>
    colorbar()
  File "C:\Python25\Lib\site-packages\matplotlib\pyplot.py", line 1129, in
colorbar
    ret = gcf().colorbar(mappable, cax = cax, ax=ax, **kw)
  File "C:\Python25\Lib\site-packages\matplotlib\figure.py", line 956, in
colorbar
    cb = cbar.Colorbar(cax, mappable, **kw)
  File "C:\Python25\Lib\site-packages\matplotlib\colorbar.py", line 558, in
__init__
    mappable.autoscale_None() # Ensure mappable.norm.vmin, vmax
AttributeError: 'NoneType' object has no attribute 'autoscale_None'

I am not sure how the mappable, ax, and cax options work.

Try:

ax2 = axes([0.2, 0.1, 0.6, 0.8], axisbg='w')
ax2.fill([x1,x2,x2,x1], [y1,y1,y2,y2], fc='None', ec='r')
ax2.pcolormesh(X, Y, newa, shading='flat', cmap=cm.YlOrRd)#gray_r)
colorbar()
ax2.axvline(x=0, color='gray', linestyle='--')
ax2.axhline(y=0, color='gray', linestyle='--')
ax2.plot([offaxisX], [offaxisY], 'r+', mew=1)

or:

ax2 = axes([0.2, 0.1, 0.6, 0.8], axisbg='w')
ax2.fill([x1,x2,x2,x1], [y1,y1,y2,y2], fc='None', ec='r')
mappable = ax2.pcolormesh(X, Y, newa, shading='flat', cmap=cm.YlOrRd)#gray_r)
ax2.axvline(x=0, color='gray', linestyle='--')
ax2.axhline(y=0, color='gray', linestyle='--')
ax2.plot([offaxisX], [offaxisY], 'r+', mew=1)
colorbar(mappable, ax=ax2)

or:

ax2 = axes([0.2, 0.1, 0.6, 0.8], axisbg='w')
ax2.fill([x1,x2,x2,x1], [y1,y1,y2,y2], fc='None', ec='r')
mappable = ax2.pcolormesh(X, Y, newa, shading='flat', cmap=cm.YlOrRd)#gray_r)
ax2.axvline(x=0, color='gray', linestyle='--')
ax2.axhline(y=0, color='gray', linestyle='--')
ax2.plot([offaxisX], [offaxisY], 'r+', mew=1)
cax = axes([0.65, 0.1, 0.2, 0.8], axisbg='w')
colorbar(mappable, cax=cax)

···

On Friday 20 June 2008 5:40:04 pm Bryan Fodness wrote:

Bryan Fodness wrote:

Is there a way to get the colorbar to work with an axes instance.
ax2 = axes([0.2, 0.1, 0.6, 0.8], axisbg='w')
ax2.fill([x1,x2,x2,x1], [y1,y1,y2,y2], fc='None', ec='r')
ax2.pcolormesh(X, Y, newa, shading='flat', cmap=cm.YlOrRd)#gray_r) ax2.axvline(x=0, color='gray', linestyle='--')
ax2.axhline(y=0, color='gray', linestyle='--')
ax2.plot([offaxisX], [offaxisY], 'r+', mew=1)
ax2.colorbar()
AttributeError: 'Axes' object has no attribute 'colorbar'
colorbar()
, line 499, in <module>
    colorbar()
  File "C:\Python25\Lib\site-packages\matplotlib\pyplot.py", line 1129, in colorbar
    ret = gcf().colorbar(mappable, cax = cax, ax=ax, **kw)
  File "C:\Python25\Lib\site-packages\matplotlib\figure.py", line 956, in colorbar
    cb = cbar.Colorbar(cax, mappable, **kw)
  File "C:\Python25\Lib\site-packages\matplotlib\colorbar.py", line 558, in __init__
    mappable.autoscale_None() # Ensure mappable.norm.vmin, vmax
AttributeError: 'NoneType' object has no attribute 'autoscale_None'
I am not sure how the mappable, ax, and cax options work.

You want pylab.colorbar() or figure.colorbar(). (The pylab version is just a simple wrapper around figure.colorbar()) In this case, I think you want to use the ax keyword for colorbar, as in:
pylab.colorbar(ax=ax2)

The ax keyword argument specifies an the axes for which you want the colorbar drawn. The cax keyword argument specifies the axes in which you want the colorbar drawn. If you don't specifiy cax, space will be taken from the ax keyword argument (or the current axes).

Ryan

···

--
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma

Bryan Fodness wrote:

Is there a way to get the colorbar to work with an axes instance.
ax2 = axes([0.2, 0.1, 0.6, 0.8], axisbg='w')
ax2.fill([x1,x2,x2,x1], [y1,y1,y2,y2], fc='None', ec='r')
ax2.pcolormesh(X, Y, newa, shading='flat', cmap=cm.YlOrRd)#gray_r) ax2.axvline(x=0, color='gray', linestyle='--')
ax2.axhline(y=0, color='gray', linestyle='--')
ax2.plot([offaxisX], [offaxisY], 'r+', mew=1)
ax2.colorbar()
AttributeError: 'Axes' object has no attribute 'colorbar'

Bryan,

A colorbar is a plot in its own axes, so just as a figure has an "add_subplot" method to add a general-purpose axes, it has a "colorbar" method to add a special-purpose axes with a colorbar in it.

One way to modify your example is something like this (untested):

ax2 = axes([0.2, 0.1, 0.6, 0.8], axisbg='w')
ax2.fill([x1,x2,x2,x1], [y1,y1,y2,y2], fc='None', ec='r')
# Save the "mappable" returned by pcolormesh:
pcm = ax2.pcolormesh(X, Y, newa, shading='flat', cmap=cm.YlOrRd)#gray_r)
ax2.axvline(x=0, color='gray', linestyle='--')
ax2.axhline(y=0, color='gray', linestyle='--')
ax2.plot([offaxisX], [offaxisY], 'r+', mew=1)
# Call the figure method, give it the mappable:
ax2.get_figure().colorbar(pcm, ax=ax2)

This will steal space from ax2 to make the colorbar axes. If you want to make the colorbar axes yourself, called ax_cbar, for example, then
the last line would be

ax2.get_figure().colorbar(pcm, cax=ax_cbar)

Of course, if you make the figure at the start, and save the reference to it, then you can use that reference in place of "ax2.get_figure()"

Eric

···

colorbar()
, line 499, in <module>
    colorbar()
  File "C:\Python25\Lib\site-packages\matplotlib\pyplot.py", line 1129, in colorbar
    ret = gcf().colorbar(mappable, cax = cax, ax=ax, **kw)
  File "C:\Python25\Lib\site-packages\matplotlib\figure.py", line 956, in colorbar
    cb = cbar.Colorbar(cax, mappable, **kw)
  File "C:\Python25\Lib\site-packages\matplotlib\colorbar.py", line 558, in __init__
    mappable.autoscale_None() # Ensure mappable.norm.vmin, vmax
AttributeError: 'NoneType' object has no attribute 'autoscale_None'
I am not sure how the mappable, ax, and cax options work.