DIscretization of colorbar

Claas Teichmann wrote:

Hi David and Eric,

one year ago, you discussed a discretazation of the colorbar with
imshow() in matplotlib-users. The topic was "DIscretization of
colorbar". Did you succeed in using a discrete colorbar?

Yes, I completely rewrote the colorbar code, and it is now quite flexible.

I put a request to the mailinglist with the topic "Wrong
colorbar-ticks in imshow-colorbar with 10 colors". Maybe one of you
already got the answer?

Many greetings!

Claas :slight_smile:

Assuming you have a reasonably recent version of mpl, you can modify your colorbar call this way:

colorbar(ticks=linspace(im.norm.vmin, im.norm.vmax, 11))

(This is for your example with a 10-entry colormap.)

Because of a default parameter that is not exposed, it will label only every second color boundary in the example from your earlier message. To make it label every boundary, you could use

from matplotlib import ticker
ticks = linspace(im.norm.vmin, im.norm.vmax, 11)
tickmaker = ticker.FixedLocator(ticks, nbins=20)
# make nbins >= number of ticks
colorbar(ticks=tickmaker)

Eric

You can also take a look at the wiki
http://www.scipy.org/Cookbook/Matplotlib/ColormapTransformations

There you’ll find the code I had about colormap discretization. Maybe it does the same thing Eric discussed, however.

Cheers,
David

2007/2/6, Eric Firing <efiring@…202…>:

···

Claas Teichmann wrote:

Hi David and Eric,

one year ago, you discussed a discretazation of the colorbar with
imshow() in matplotlib-users. The topic was “DIscretization of
colorbar”. Did you succeed in using a discrete colorbar?

Yes, I completely rewrote the colorbar code, and it is now quite flexible.

I put a request to the mailinglist with the topic “Wrong
colorbar-ticks in imshow-colorbar with 10 colors”. Maybe one of you

already got the answer?

Many greetings!

Claas :slight_smile:

Assuming you have a reasonably recent version of mpl, you can modify
your colorbar call this way:

colorbar(ticks=linspace(
im.norm.vmin, im.norm.vmax, 11))

(This is for your example with a 10-entry colormap.)

Because of a default parameter that is not exposed, it will label only
every second color boundary in the example from your earlier message.

To make it label every boundary, you could use

from matplotlib import ticker
ticks = linspace(im.norm.vmin, im.norm.vmax, 11)
tickmaker = ticker.FixedLocator(ticks, nbins=20)

make nbins >= number of ticks

colorbar(ticks=tickmaker)

Eric


Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.

Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642


Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Hi Eric,

Great that it works!! This is what I was looking for!

There is one thing left, which is that the tick labels are not exactly
at the boundary between the colors. (It is no problem for me, but I am
interested where it comes from). The script below shows that the
colorswitch is not exactly at an "even" number. The values
-0.022000001 and -0.022 are colored with the same color, whereas
-0.02200001 has a different color. I don't know whether this is the
reason for the ticks not beeing exactly at the intersection..?

I used the newest matplotlib version from the svn-repository.

I would use a script in the following way:
from pylab import *
from matplotlib import ticker

delta = 0.01
x = arange(-3.0, 3.0, delta)
y = arange(-3.0, 3.0, delta)
X,Y = meshgrid(x, y)
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = Z2 - Z1 # difference of Gaussians

Z[20,20]=-0.022
Z[20,25]=-0.022000001
Z[20,30]=-0.02200001
Z[20,35]=-0.0220001
Z[20,40]=-0.022001
Z[20,45]=-0.02201

cmap = cm.get_cmap('jet', 10) # 10 discrete colors

#### Set vmin and vmax beforehand
im = imshow(Z, cmap=cmap, vmin=-0.15, vmax=0.17, interpolation='nearest')

colorbar(ticks=linspace(im.norm.vmin, im.norm.vmax, 11)) # 11 tick labels

Thanks!

Claas :slight_smile:

Claas Teichmann wrote:

Hi Eric,

Great that it works!! This is what I was looking for!

There is one thing left, which is that the tick labels are not exactly
at the boundary between the colors. (It is no problem for me, but I am
interested where it comes from). The script below shows that the
colorswitch is not exactly at an "even" number. The values
-0.022000001 and -0.022 are colored with the same color, whereas
-0.02200001 has a different color. I don't know whether this is the
reason for the ticks not beeing exactly at the intersection..?

When I run the script below I don't see what you are describing above; but I may not be looking in the right place, and it would not surprise me if there are little anomalies like this, because we are dealing with floating point arithmetic.

I do see (and had noticed before) that the colorbar ticks look a tiny bit high. I don't know whether this is a bug or whether it is a consequence of the fact that colors are assigned to ranges that include the lower limit but not the upper limit, and/or floating point arithmetic, and/or the resolution of the display. Or it may be because of some floating point fudge factors that I put into the colorbar code. Maybe these could be improved.

Eric