Skip to content Skip to sidebar Skip to footer

Force Use Of Scientific Style For Basemap Colorbar Labels

String formatting can by used to specify scientific notation for matplotlib.basemap colorbar labels: cb = m.colorbar(cs, ax=ax1, format='%.4e') But then each label is scientifical

Solution 1:

There's no one-line method, but you can do this by updating the colorbar's formatter and then calling colorbar.update_ticks().

import numpy as np
import matplotlib.pyplot as plt

z = np.random.random((10,10))

fig, ax = plt.subplots()
im = ax.imshow(z)
cb = fig.colorbar(im)

cb.formatter.set_powerlimits((0, 0))
cb.update_ticks()

plt.show()

enter image description here

The reason for the slightly odd way of doing things is that a colorbar actually has statically assigned ticks and ticklabels. The colorbar's axes (colorbar.ax) actually always ranges between 0 and 1. (Therefore, altering colorbar.ax.yaxis.formatter doesn't do anything useful.) The tick positions and labels are calculated from colorbar.locator and colorbar.formatter and are assigned when the colorbar is created. Therefore, if you need precise control over a colorbar's ticks/ticklables, you need to explicitly call colorbar.update_ticks() after customizing how the ticks are displayed. The colorbar's convenience functions do this for you behind the scenes, but as far as I know, what you want can't be done through another method.


Post a Comment for "Force Use Of Scientific Style For Basemap Colorbar Labels"