2

I would like to multiply my y-axis ticks (as you can see below) so that instead of 0.25 to 2 they show a range of values from 2500 up to 20000.

How do I do that?

desired output

Anton Menshov
  • 2,091
  • 11
  • 30
  • 48

1 Answers1

1

You can use this order of magnitude formatter:

# https://stackoverflow.com/a/42658124/13138364
import matplotlib.ticker

class OOMFormatter(matplotlib.ticker.ScalarFormatter):
    def __init__(self, order=0, fformat='%1.1f', offset=True, mathText=True):
        self.oom = order
        self.fformat = fformat
        matplotlib.ticker.ScalarFormatter.__init__(self,useOffset=offset,useMathText=mathText)
    def _set_order_of_magnitude(self):
        self.orderOfMagnitude = self.oom
    def _set_format(self, vmin=None, vmax=None):
        self.format = self.fformat
        if self._useMathText:
            self.format = r'$\mathdefault{%s}$' % self.format

Toy example changing 1e7 to 1e3:

x = np.arange(100)
y = np.random.rand(100) * 2e7

# plot with `ax` handle
fig, ax = plt.subplots()
ax.plot(x, y)

# format order of magnitude of `ax.yaxis`
order = 3
ax.yaxis.set_major_formatter(OOMFormatter(order, '%1.1f'))

Before/after:

before/after formatting order of magnitude

tdy
  • 26,545
  • 9
  • 43
  • 50