Home > Software design >  How to set scientific notation limits for colorbar?
How to set scientific notation limits for colorbar?

Time:09-23

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 enter image description here

CodePudding user response:

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()

enter image description here

  • Related