# help on colorbar usage

Eric Firing wrote:

Thanks Eric - I just wanted to make sure I wasn't missing some obvious way to do it.

Jeff,

A possible trick would be to make a very skinny subplot (using custom sizing and positioning) and use contourf itself to make the colorbar in it. I think this is actually the way I am doing it in Matlab. (A colleague and I greatly customized the Matlab colorbar for this sort of thing a long time ago.)

Eric

Eric/Phil: Here is a hack that produces colorbars with discrete contour intervals for use with contourf. It's in the form of a patch for pylab.py and figure.py (based on 0.83.2). When pylab.colorbar is called, it checks to see if the current image was created by contourf, and if so calls figure.colobar_contourf (which uses contourf instead of imshow to make the colorbar). Here's a test script:

"""test script for contourf colorbar"""
from pylab import *
def func3(x,y):
return (1- x/2 + x**5 + y**3)*exp(-x**2-y**2)
dx, dy = 0.05, 0.05
X,Y = meshgrid(arange(-3.0,3.0001,dx),arange(-3.0,3.0001,dy))
Z = func3(X, Y)
levels = linspace(-1.2,1.2,13)
l,c = contour (X, Y, Z, levels, linewidths=0.5, colors='k')
clabel(c,l,fmt='%4.2f')
l,c = contourf(X, Y, Z, levels, cmap=cm.jet)
colorbar(tickfmt='%4.2f',orientation='horizontal') # horiz colorbar
colorbar(tickfmt='%4.2f',orientation='vertical') # vert colorbar
axis([-3,3,-3,3])
show()

This doesn't address the other two of Phil's requests (triangles at the ends, and constant intervals on the colorbar for non-constant contour levels), but I think it's a start.

-Jeff

pylab.patch (479 Bytes)

figure.patch (3.32 KB)

ยทยทยท

--
Jeffrey S. Whitaker Phone : (303)497-6313
Meteorologist FAX : (303)497-6449
NOAA/OAR/CDC R/CDC1 Email : Jeffrey.S.Whitaker@...259...
325 Broadway Office : Skaggs Research Cntr 1D-124
Boulder, CO, USA 80303-3328 Web : Jeffrey S. Whitaker: NOAA Physical Sciences Laboratory