Does anyone know how to change the distance of the colorbar ticklabels with respect to the colorbar in Python/Matplotlib? Thanks!
So for example, here I would like to move the 0, 0.5 and 1.0 to the left or right with respect to the colorbar:

Borrowing from the standalone colourbars example, this shows how to
change the spacing between the tickmarks (just specify them manually)
change the distance of the ticklabels to the colourbar. The trick here is to obtain the relevant axis (cb1.ax) and apply the right tick parameter (pad=...).
from matplotlib import pyplot
import matplotlib as mpl
fig = pyplot.figure(figsize=(8,3))
ax1 = fig.add_axes([0.05, 0.70, 0.9, 0.15])
ax2 = fig.add_axes([0.05, 0.35, 0.9, 0.15])
cmap = mpl.cm.cool
norm = mpl.colors.Normalize(vmin=5, vmax=10)
cb1 = mpl.colorbar.ColorbarBase(ax1, cmap=cmap, norm=norm,
orientation='horizontal')
cb1.set_label('Negative spacing')
cb1.set_ticks([5.5, 7.5, 9.5])
cb1.ax.xaxis.set_tick_params(pad=-15)
cb2 = mpl.colorbar.ColorbarBase(ax2, cmap=cmap, norm=norm,
orientation='horizontal')
cb2.set_label('Far away')
cb2.set_ticks([5.5, 7.5, 9.5])
cb2.ax.xaxis.set_tick_params(pad=30)
pyplot.savefig('colorbar-ticklabels.png')

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With