I am trying to put a scientific notation in all ticks of colorbar (the figure below). I could make it by colorbar(cs, format='%.2e')
. The problem is that after I set tick labels using the following lines
cbar.set_ticks(levels)
cbar.set_ticklabels(levels)
the lower limit of notation seems to change to -5 and it shows normal digits for -4 like the figure below (note the tick label 0.00036). I know that one can change scientific notation limits for axes through ticklabel_format. I am looking for the same option for colorbar to change it from -5 to -4.
Any help is appreciated.
Update1:
I have used the following codes but no luck yet.
cbformat = matplotlib.ticker.ScalarFormatter()
cbformat.set_powerlimits((-12,12))
cbformat.set_scientific('%.2e')
cbar = m.colorbar(cs, location='right',format=cbformat, pad="10%")
cbar.set_ticks(levels)
cbar.set_ticklabels(np.round(levels,7))
Update 2:
To reproduce the problem, please use the following codes I copied from this post
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import ticker
from matplotlib.colors import LogNorm
z = np.random.lognormal(mean=10, sigma=3, size=(10,10))
z=z*10**-11
levels = np.linspace(z.min(), np.quantile(z,0.8), 8)
levels = np.append(levels, np.quantile(z,0.85))
levels = np.append(levels, np.quantile(z,0.9))
levels = np.append(levels, np.quantile(z,0.95))
levels = np.append(levels, z.max())
fig, ax = plt.subplots()
# levels=np.logspace(np.log10(np.min(z)),np.log10(np.max(z)),100)
plot = ax.contourf(z, levels, norm=LogNorm())
cbar = fig.colorbar(plot, location='right',format='%.2e')
cbar.set_ticks(levels)
cbar.set_ticklabels(np.round(levels,9))
plt.show()
you can specify the ticks when creating the colorbar, then the cbformat
works.
IIUC the following plot is what you want:
fig, ax = plt.subplots()
plot = ax.contourf(z, levels, norm=LogNorm())
cbformat = ticker.ScalarFormatter()
cbformat.set_scientific('%.2e')
cbformat.set_powerlimits((-4,12))
cbformat.set_useMathText(True)
cbar = fig.colorbar(plot, ticks=levels, location='right', format=cbformat)
plt.show()