End ticks in colorbar - matplotlib
Asked Answered
H

2

6

I am trying to plot a colorbar in Python using the matplotlib libraries. The plot works well, but the color bar wont show the end ticks on the color bar.

Using the following command plots the colorbar properly, but I only have ticks ranging from -1.6 to +1.6 ( the top and bottom ticks are absent). The range of my data is from -2 to +2.

fig.colorbar(surf, shrink=1, aspect=12)

This is seen in the following figure:

colorbar

I tried using the command:

cbar.set_ticks([-2,-1,0,1,2]);

But that also failed to get the correct result.

UPDATE:

I tried using the ticks=[-2,0,2] parameter, but that did not work as well. This is a snippet of my plotting code:

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')
CHI = np.linspace(-45, 45, 35);
M = np.linspace(0, 1, 35)
CHI, M = np.meshgrid(CHI, M)
R = 2*M*np.sin(  2 * np.deg2rad(CHI) )
surf = ax.plot_surface(CHI, M, R, rstride=1, cstride=1, cmap=cm.hsv,linewidth=0, antialiased=False)
ax.set_xlim(-45,45)
cbar = plt.colorbar(surf, shrink=1, aspect=12, ticks=[-2,-1,0,1,2])
plt.show()

This produces the following plot: PLot wrong bar

As seen, the colorbar lacks the end ticks, viz. -2 and 2

Heiskell answered 26/7, 2013 at 13:13 Comment(1)
Did you remember to call plt.draw() after set_ticks? Most of the functions do not automatically trigger re-draws.Worser
W
8
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')
CHI = np.linspace(-45, 45, 35);
M = np.linspace(0, 1, 35)
CHI, M = np.meshgrid(CHI, M)
R = 2*M*np.sin(  2 * np.deg2rad(CHI) )
surf = ax.plot_surface(CHI, M, R, rstride=1, cstride=1, cmap=cm.hsv,linewidth=0, antialiased=False)
surf.set_clim([-2, 2]) # <- this is the important line
ax.set_xlim(-45,45)
cbar = plt.colorbar(surf, shrink=1, aspect=12, ticks=[-2,-1,0,1,2])
plt.show()

Something is going wrong with the auto-scaling (I would guess issues with floating point equality tests), if you explicitly set the clim to [-2, 2] it works.

Worser answered 28/7, 2013 at 22:3 Comment(2)
Thank you so much. For the solution and for explaining the probable cause.Heiskell
@eternalDreamer Could you submit this as a bug report on github?Worser
I
5

You should be able to set the ticks parameter when calling plt.colorbar:

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
cmap = plt.get_cmap('jet')
data = np.random.randint(-2,3, size=(10,10))
im = ax.imshow(data, interpolation='nearest', cmap=cmap)
cbar = plt.colorbar(im, ticks=[-2,-1,0,1,2])
plt.show()

enter image description here

Irrationality answered 26/7, 2013 at 13:43 Comment(1)
This does not work for me. Even if I use this: cbar = plt.colorbar(surf, shrink=1, aspect=12, ticks=[-2,-1,0,1,2]) the top and bottom most labels are not rendered. ( Please see updated question)Heiskell

© 2022 - 2024 — McMap. All rights reserved.