85

I'm using Matplotlib in Python to plot simple x-y datasets. This produces nice-looking graphs, although when I "zoom in" too close on various sections of the plotted graph using the Figure View (which appears when you execute plt.show() ), the x-axis values change from standard number form (1050, 1060, 1070 etc.) to scientific form with exponential notation (e.g. 1, 1.5, 2.0 with the x-axis label given as +1.057e3).

I'd prefer my figures to retain the simple numbering of the axis, rather than using exponential form. Is there a way I can force Matplotlib to do this?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
IanRoberts
  • 2,846
  • 5
  • 26
  • 33
  • 1
    duplicate of http://stackoverflow.com/questions/11855363/how-to-remove-relative-shift-in-matplotlib-axis/11858063#11858063 – tacaswell Feb 05 '13 at 16:13
  • Obsolete answers. For an updated version, see here: https://stackoverflow.com/q/28371674/8881141 – Mr. T Feb 01 '22 at 21:40

6 Answers6

103

The formatting of tick labels is controlled by a Formatter object, which assuming you haven't done anything fancy will be a ScalerFormatterby default. This formatter will use a constant shift if the fractional change of the values visible is very small. To avoid this, simply turn it off:

plt.plot(arange(0,100,10) + 1000, arange(0,100,10))
ax = plt.gca()
ax.get_xaxis().get_major_formatter().set_useOffset(False)
plt.draw()

If you want to avoid scientific notation in general,

ax.get_xaxis().get_major_formatter().set_scientific(False)

Can control this with globally via the axes.formatter.useoffset rcparam.

tacaswell
  • 84,579
  • 22
  • 210
  • 199
  • 1
    Thanks, that works. Apologies for the duplicate question too - I did search before posting it, but my searches included terms such as "scientific notation" and "exponential form" rather than "relative axis shifts", which is what the previous user used. – IanRoberts Feb 05 '13 at 16:37
  • 2
    Is there a way to permanently force this change? I've never found the scientific notation axes and always end up turning them off. I couldn't see an rcParam for this though. – IanRoberts Jul 30 '14 at 14:54
  • 1
    @IanRoberts See update it is the `axes.formatter.useoffset` rcparam – tacaswell Jan 15 '15 at 16:19
  • 3
    Great, thanks! I'd personally prefer to see this set to False by default, since it seems to confuse many and benefit few. – IanRoberts Jan 19 '15 at 20:48
  • True, good point. That was a fairly unsubstantiated claim, although I have seen quite a few people asking how to disable this. Anyway, this fix solves it nicely so I can forget about this for now... Cheers – IanRoberts Jan 19 '15 at 21:11
  • I can't get this to work; see my question here: http://stackoverflow.com/questions/40336020/prevent-axes-from-being-in-scientific-notation-powers-of-10-using-matplotlib-i – Gabriel Staples Oct 31 '16 at 03:34
35

You can use a simpler command to turn it off:

plt.ticklabel_format(useOffset=False)
Eki
  • 641
  • 5
  • 14
  • You might need to specify the axis with `axis='x'` for instance, in case you get an `AttributeError` about `ScalarFormatters`. – João Bravo Sep 21 '21 at 14:43
20

You can use something like:

from matplotlib.ticker import ScalarFormatter, FormatStrFormatter

ax.xaxis.set_major_formatter(FormatStrFormatter('%.0f'))
Mathias711
  • 6,568
  • 4
  • 41
  • 58
Animesh Saxena
  • 449
  • 4
  • 11
  • 4
    This is the only solution that worked for me whilst using a log axis. Other options I tried all raised attribute errors, e.g. `AttributeError: 'LogFormatterSciNotation' object has no attribute 'set_scientific'` – tsherwen Aug 24 '20 at 15:47
7

Use the following command: ax.ticklabel_format(useOffset=False, style='plain')

If you are using a subplot, you may experience the AttributeError: This method only works with the ScalarFormatter in which case you would add axis='y' like the below. You can change 'y' to the axis with the issues.

ax1.ticklabel_format(useOffset=False, style='plain', axis='y')

Source question and answer here. Note, the axis 'y' command use is hidden in the answer comments.

Mauro
  • 307
  • 4
  • 8
5

I have used below code before the graphs, and it worked seamless for me..

plt.ticklabel_format(style='plain')
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
0

Exactly I didn't want scientific numbers to be shown when I zoom in, and the following worked in my case too. I am using Lat/Lon in labeling where scientific form doesn't make sense.

plt.ticklabel_format(useOffset=False)
Marzi Heidari
  • 2,660
  • 4
  • 25
  • 57