1

I am trying to plot a colorbar with scientific and without offset notation. Setting colorbar.formatter.set_useOffset(False) (https://stackoverflow.com/a/29280475/10734962) doesn't work.

import matplotlib.pyplot as plt
import numpy as np

CMIN, CMAX = 5e15, 1e16

# Create axes
figure_h, axes_h = plt.subplots(3, figsize=(5, 8))
   
# Create data
data = np.random.uniform(CMIN, CMAX, (10, 10))
   
# Plot -- default
plot_h = axes_h[0].imshow(data, cmap='jet', clim=(CMIN, CMAX))
c = plt.colorbar(plot_h, ax=axes_h[0])
   
# Plot -- manual ticks
plot_h = axes_h[1].imshow(data, cmap='jet', clim=(CMIN, CMAX))
colorbar_h = plt.colorbar(plot_h, ax=axes_h[1])
colorbar_h.set_ticks(colorbar_h.get_ticks())  # To prevent 'UserWarning: set_ticks() must have been called.'
colorbar_h.set_ticklabels([f'{f:.0e}' for f in colorbar_h.get_ticks()])
   
# Plot -- no offset notation
plot_h = axes_h[2].imshow(data, cmap='jet', clim=(CMIN, CMAX))
colorbar_h = plt.colorbar(plot_h, ax=axes_h[2])
colorbar_h.formatter.set_useOffset(False)
colorbar_h.update_ticks()

plt.show()

Figure

I would like to get a colorbar that looks similar to the second one, but without specifying the ticklabels explicitly.

I also tried setting rcParams['axes.formatter.useoffset'] = False, without success.

I am using matplotlib version 3.3.1

DavidG
  • 24,279
  • 14
  • 89
  • 82
b1000
  • 87
  • 1
  • 7

0 Answers0