Creating non linear colorbar Colorbar


I am trying to generate on the fly a non linear rainbow colorbar as in
picture Colorbar below.

I use a slider to automatically modify the aspect of the colorbar (see code
This works fine except that I want to have a different behaviour of the
colorbar when moving the slider:
My wish is to have the limits of the colorbar remain fixed (cmin and cmax),
but the rainbow range should be shrinked (though the tick label of the
colorbar should not be modified).

I am seaching around using matplotlib.colors.LinearSegmentedColormap without
any success.
Should in this case the colormap be completly re created each time the
slider moves, or is it possible using any kwargs to automatically change the
behaviour of the colorbar as I wanted ?

Many thanks in advance for any help...

See example below, where the colorbar cmax limit is changed, whereas I
wanted that it remains fixed and the shape of the colorbar rainbow

from matplotlib.pylab import *
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider

sizeX = 4
sizeY = 21

dims = ( sizeY, sizeX )

data = zeros( dims )
for x in range( 0, sizeX ):
  for y in range( 0, sizeY ):
    data[ y, x ] = y

figure = plt.figure()
axes = figure.add_subplot( 111 )
figure.subplots_adjust(left=0.25, bottom=0.25)

cax = axes.matshow( data, aspect = 'auto', interpolation = 'nearest' )
col = figure.colorbar( cax, spacing='proportional', extend='min' )

vmax = 50
axmax = figure.add_axes([0.25, 0.15, 0.65, 0.03],
smax = Slider( axmax, 'Max', 0, vmax, valinit = vmax )
cax.set_clim( vmin = 0, vmax = vmax )

def update(val):
    cax.set_clim( vmin = 0, vmax = smax.val )
smax.on_changed( update ) Colorbar.png


View this message in context:
Sent from the matplotlib - users mailing list archive at