I've stumbled onto a bug in colorbar() when displaying an image with a
nonlinear normalization (using a recent CVS version of mpl). If one
subclasses matplotlib.colors.normalize and uses a nonlinear function in the
__call__() method, then colorbar() will mismatch colors and data values in
Some code illustrating a test case is attached. Here, I use the sqrt()
function to normalize some data in the domain (0, 10) to the range (0, 1)
[f(x)=sqrt(x/10)]. I display an image which consists of a linear ramp, each
value given by the abcissa. The normalization function y=f(x) is overplotted
for reference. imshow() applies the normalization before looking up the
colormap value, as expected (colors are bunched to the left). However, note
the values in the colorbar annotation do not correspond to the data values!
For example, pre-normalization data value 4 (x-axis) is correctly colored as
yellow, however the color bar erroneously lists that value as cyan (which is
the color where y=4/10=.4).
The error is that colorbar() assumes linearity over the normalization domain.
Ultimately, I think I'd like a choice as to whether to stretch colors in the
colorbar with a linear sampling of the data domain, or keep the color
sequence linear and invert the normalization step to determine the tick
values. Has anyone encountered and/or coded a solution for this?
colorbar_test.py (911 Bytes)