reverse axis on colorbar, and showing under- and over-colors

Hi,

My searching for reverse in the documentation and gallery came up empty,
so I'll try it here:

I have an image with cloud pressures, 1000 at the surface, 200 at the
top of the atmosphere. I'd like to reverse the axis, i.e. 1000 at the
bottom of the scale, 200 at the top. How should I approach this?

Related: I have set colors for under- and over-values. I'd like to
display a small patch of these colors at either end of the scale. The
bad values are clear enough.

Best,

Maarten

···

--
KNMI, De Bilt
T: 030 2206 747
E: Maarten.Sneep@...3329...
Room B 2.42

Hi,

I did solve my own question. For posterity, and perhaps for a more
elegant solution, I post my solution here.

I have an image with cloud pressures, 1000 at the surface, 200 at the
top of the atmosphere. I'd like to reverse the axis, i.e. 1000 at the
bottom of the scale, 200 at the top. How should I approach this?

Instead of using a figure.colorbar() call, I instantiate ColorbarBase
directly. I needed an axes object anyway to use a single colorbar fro a
two-subplot figure. The ColorbarBase gets the reversed colormap compared
to the image.

fig = plt.figure()
norm=Normalize(vmin=200.0, vmax=1000.0, clip=False)
cmap = cm.jet
cmap.set_bad(color=(0.75,0.75, 0.75))
cmap.set_over(color=(1.0, 1.0, 1.0))
cmap.set_under(color=(0.5, 0.5, 0.5))
cmap_bar = cm.jet_r
# note that under and over are reversed.
cmap_bar.set_bad(color=(0.75,0.75, 0.75))
cmap_bar.set_under(color=(1.0, 1.0, 1.0))
cmap_bar.set_over(color=(0.5, 0.5, 0.5))

# make subplots
ax1 = fig.add_subplot(211)
ax1.imshow(cp1, origin='lower', cmap=cmap,
    interpolation='nearest', aspect='auto', norm=norm)
ax2 = fig.add_subplot(212)
ax2.imshow(cp2, origin='lower', cmap=cmap,
    interpolation='nearest', aspect='auto', norm=norm)

# make room for colorbar
plt.subplots_adjust(left=0.07, hspace=0.25, right=0.825)
cbar_ax = plt.axes([0.875, 0.1, 0.025, 0.8])
tickvals = [200, 400, 600, 800, 1000]
cbar = ColorbarBase(cbar_ax, cmap=cmap_bar,
    ticks=tickvals, extend='both', norm=norm)
# I use latex formatting, original is more interesting.
labels = ['%d' % v for v in tickvals]
cbar.ax.set_yticklabels(lblslabels[::-1])

Dirty: yes, and it only works because of the equidistant labels. A more
elegant solution is appreciated.

Related: I have set colors for under- and over-values. I'd like to
display a small patch of these colors at either end of the scale. The
bad values are clear enough.

extend='both' keyword to the ColorbarBase() call, or the colorbar()
call.

Maarten

···

On Tue, 2010-10-26 at 10:56 +0200, Maarten Sneep wrote:

Hi,

I did solve my own question. For posterity, and perhaps for a more
elegant solution, I post my solution here.

I have an image with cloud pressures, 1000 at the surface, 200 at the
top of the atmosphere. I'd like to reverse the axis, i.e. 1000 at the
bottom of the scale, 200 at the top. How should I approach this?

Instead of using a figure.colorbar() call, I instantiate ColorbarBase
directly. I needed an axes object anyway to use a single colorbar fro a
two-subplot figure. The ColorbarBase gets the reversed colormap compared
to the image.

fig = plt.figure()
norm=Normalize(vmin=200.0, vmax=1000.0, clip=False)
cmap = cm.jet
cmap.set_bad(color=(0.75,0.75, 0.75))
cmap.set_over(color=(1.0, 1.0, 1.0))
cmap.set_under(color=(0.5, 0.5, 0.5))
cmap_bar = cm.jet_r
# note that under and over are reversed.
cmap_bar.set_bad(color=(0.75,0.75, 0.75))
cmap_bar.set_under(color=(1.0, 1.0, 1.0))
cmap_bar.set_over(color=(0.5, 0.5, 0.5))

# make subplots
ax1 = fig.add_subplot(211)
ax1.imshow(cp1, origin='lower', cmap=cmap,
     interpolation='nearest', aspect='auto', norm=norm)
ax2 = fig.add_subplot(212)
ax2.imshow(cp2, origin='lower', cmap=cmap,
     interpolation='nearest', aspect='auto', norm=norm)

# make room for colorbar
plt.subplots_adjust(left=0.07, hspace=0.25, right=0.825)
cbar_ax = plt.axes([0.875, 0.1, 0.025, 0.8])
tickvals = [200, 400, 600, 800, 1000]
cbar = ColorbarBase(cbar_ax, cmap=cmap_bar,
     ticks=tickvals, extend='both', norm=norm)
# I use latex formatting, original is more interesting.
labels = ['%d' % v for v in tickvals]
cbar.ax.set_yticklabels(lblslabels[::-1])

Dirty: yes, and it only works because of the equidistant labels. A more
elegant solution is appreciated.

Illustrated using ipython -pylab:

z = rand(10, 12)
im = imshow(z)
cbar = colorbar(im, extend='both')
cbar.ax.invert_yaxis() # This is the key method.
draw()

Related: I have set colors for under- and over-values. I'd like to
display a small patch of these colors at either end of the scale. The
bad values are clear enough.

extend='both' keyword to the ColorbarBase() call, or the colorbar()
call.

That is indeed the right way to do it.

Eric

···

On 10/26/2010 04:50 AM, Maarten Sneep wrote:

On Tue, 2010-10-26 at 10:56 +0200, Maarten Sneep wrote:

Maarten