End ticks in colorbar - matplotlib
enthought, matplotlib, numpy, python, scipy
Solution
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.
Problem
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: 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: As seen, the colorbar lacks the end ticks, viz. -2 and 2