Colorbar Ticks

Hello,

I have the problem, that sometimes the first and last ticklabel of a
colorbar is not drawn. E.g. if I create a colorbar through
fig.colorbar(p3,orientation='horizontal',ticks=np.arange(0.0,8,2))
I only get ticklabels at 2,4,6, but I would like it to have 0,2,4,6,8 and I
don't know how to control this behaviour. If I do
cb=fig.colorbar(p3,orientation='horizontal',ticks=np.arange(0.0,8,2))
cb.ax.xaxis.set_ticks(np.arange(0,8,2))
the colorbar vanishes and instead I have 4 ticks with the labels
0.8,1.6,2.4,3.2

Thank you for your help,
Markus

···

--
View this message in context: http://www.nabble.com/Colorbar-Ticks-tp23712716p23712716.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

I should perhaps mention, that if i try
cbar=fig.colorbar(p1,orientation='horizontal',ticks=[0.0,2.0,4.0,6.0,8.0])
cbar.ax.set_xticklabels(['0', '2', '4','6','8'])
the colorbar is drawn correctly, but I get the label 0 at position 2, the
label 2 at position 4 and 4 at pos. 6, the labels at the end are not
drawn...

···

--
View this message in context: http://www.nabble.com/Colorbar-Ticks-tp23712716p23712944.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

marcusantonius wrote:

Hello,

I have the problem, that sometimes the first and last ticklabel of a
colorbar is not drawn. E.g. if I create a colorbar through
fig.colorbar(p3,orientation='horizontal',ticks=np.arange(0.0,8,2))
I only get ticklabels at 2,4,6, but I would like it to have 0,2,4,6,8 and I
don't know how to control this behaviour. If I do
cb=fig.colorbar(p3,orientation='horizontal',ticks=np.arange(0.0,8,2))
cb.ax.xaxis.set_ticks(np.arange(0,8,2))
the colorbar vanishes and instead I have 4 ticks with the labels
0.8,1.6,2.4,3.2

Thank you for your help,
Markus

Markus,

The problem here is not the specification of the ticks, it is the actual range of the colorbar, which is taken from the norm object used in the color mapping. Try this (in ipython -pylab):

imshow(rand(10,10)*8, vmin=0, vmax=8)
colorbar(ticks=[0,2,4,6,8])

Alternatively, you can use the set_clim() method on any Mappable such as an image, or the pyplot clim() function, to set the limits.

Note also that to get the sequence [0,2,4,6,8] you need arange(0,9,2), not arange(0,8,2). Or you can use linspace(0,8,5) if you prefer.

Eric

marcusantonius wrote:

I should perhaps mention, that if i try
cbar=fig.colorbar(p1,orientation='horizontal',ticks=[0.0,2.0,4.0,6.0,8.0])

The ticks that it uses are taken from the list--they are the ones that are within the range of numbers mapped to colors. The list of ticks does not set that range.

cbar.ax.set_xticklabels(['0', '2', '4','6','8'])

Setting ticklabels is dangerous unless you do it based on first getting the actual ticks and using their values, or if you are otherwise sure of their values. But it should be very rare that you have to do this.

the colorbar is drawn correctly, but I get the label 0 at position 2, the
label 2 at position 4 and 4 at pos. 6, the labels at the end are not
drawn...

None of the labels correspond to their ticks in this case.

Eric

Thank you very much for your email.

Your example

imshow(rand(10,10)*8, vmin=0, vmax=8)
colorbar(ticks=[0,2,4,6,8])

works fine.

Note also that to get the sequence [0,2,4,6,8] you need arange(0,9,2),
not arange(0,8,2). Or you can use linspace(0,8,5) if you prefer.

Thank you for making me aware of that (You see that I don't have a lot of
experience in python and matplotlib).

I now specified the color range via vmin and vmax, and this solved my
problem. Thanks for the fast help

···

--
View this message in context: http://www.nabble.com/Colorbar-Ticks-tp23712716p23713404.html
Sent from the matplotlib - users mailing list archive at Nabble.com.