117

I've been trying to suppress scientific notation in pyplot for a few hours now. After trying multiple solutions without success, I would like some help.

plt.plot(range(2003,2012,1),range(200300,201200,100))
# several solutions from other questions have not worked, including
# plt.ticklabel_format(style='sci', axis='x', scilimits=(-1000000,1000000))
# ax.get_xaxis().get_major_formatter().set_useOffset(False)
plt.show()

plot

Lioness100
  • 7,858
  • 5
  • 13
  • 46
Travis Osterman
  • 1,273
  • 2
  • 9
  • 4

1 Answers1

190

In your case, you're actually wanting to disable the offset. Using scientific notation is a separate setting from showing things in terms of an offset value.

However, ax.ticklabel_format(useOffset=False) should have worked (though you've listed it as one of the things that didn't).

For example:

fig, ax = plt.subplots()
ax.plot(range(2003,2012,1),range(200300,201200,100))
ax.ticklabel_format(useOffset=False)
plt.show()

enter image description here

If you want to disable both the offset and scientific notaion, you'd use ax.ticklabel_format(useOffset=False, style='plain').


Difference between "offset" and "scientific notation"

In matplotlib axis formatting, "scientific notation" refers to a multiplier for the numbers show, while the "offset" is a separate term that is added.

Consider this example:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(1000, 1001, 100)
y = np.linspace(1e-9, 1e9, 100)

fig, ax = plt.subplots()
ax.plot(x, y)
plt.show()

The x-axis will have an offset (note the + sign) and the y-axis will use scientific notation (as a multiplier -- No plus sign).

enter image description here

We can disable either one separately. The most convenient way is the ax.ticklabel_format method (or plt.ticklabel_format).

For example, if we call:

ax.ticklabel_format(style='plain')

We'll disable the scientific notation on the y-axis:

enter image description here

And if we call

ax.ticklabel_format(useOffset=False)

We'll disable the offset on the x-axis, but leave the y-axis scientific notation untouched:

enter image description here

Finally, we can disable both through:

ax.ticklabel_format(useOffset=False, style='plain')

enter image description here

Joe Kington
  • 258,645
  • 67
  • 583
  • 455
  • 18
    Lovely answer. The one thing I had to add, in my case, was an `axis='y'` argument to the `ax.ticklabel_format` call, as my x axis is pretty dates and my y axis is floats (it was throwing errors trying to turn off scientific notation on the dates). The default for axis is `axis='both'`. – CarlRJ Jul 26 '17 at 19:31
  • 4
    I'm probably not getting why `style='plain'` does not turn off scientific notation unless combined with `useOffset=False`. In my case at least, both are required in order to get rid of the somewhat arbitrary scientific notation choice of matplotlib: `subplot.axes.ticklabel_format(style='plain', useOffset=False, axis='both')` – matanster Aug 29 '18 at 08:51
  • 3
    This only works for scatter formatter :-( – Soren Nov 10 '19 at 03:20
  • 27
    If you get `AttributeError: This method only works with the ScalarFormatter`, only one of your axis may be the problem (I had this problem due to a date/time axis.) Try specifying which axis to affect with `axis='y'`. – Kralc Apr 06 '20 at 08:21
  • How do I implement it to a normal plt.figure()? I tried using this method and while there was no error, it changed nothing in my plot. – Toma Mar 07 '21 at 02:50
  • This is great info. Is there a way to format the offset values without scientific notation? So 1,000 instead of 1e3 and 1,000,000,000 instead of 1e9 ? – Vik Mar 07 '21 at 15:44
  • @Kralc I get that error even with `axis = 'y'`... – zyy Jul 01 '21 at 04:50
  • 1
    @Toma use `plt.tickalbel_format`. If no change has been made there is probably another reason. – KeVal Sep 27 '21 at 15:25