How to prevent numbers being changed to exponential form in a plot
Asked Answered
S

6

86

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?

Saurel answered 5/2, 2013 at 16:1 Comment(2)
duplicate of #11855863Carvey
Obsolete answers. For an updated version, see here: https://mcmap.net/q/157809/-prevent-scientific-notation/8881141Nonagenarian
C
106

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.

Carvey answered 5/2, 2013 at 16:11 Comment(6)
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.Saurel
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.Saurel
@Saurel See update it is the axes.formatter.useoffset rcparamCarvey
Great, thanks! I'd personally prefer to see this set to False by default, since it seems to confuse many and benefit few.Saurel
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... CheersSaurel
I can't get this to work; see my question here: #40336520Reverential
G
37

You can use a simpler command to turn it off:

plt.ticklabel_format(useOffset=False)
Golgotha answered 5/5, 2017 at 12:15 Comment(1)
You might need to specify the axis with axis='x' for instance, in case you get an AttributeError about ScalarFormatters.Barcelona
X
20

You can use something like:

from matplotlib.ticker import ScalarFormatter, FormatStrFormatter

ax.xaxis.set_major_formatter(FormatStrFormatter('%.0f'))
Xylia answered 4/3, 2015 at 5:24 Comment(1)
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'Amatory
A
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.

Ain answered 11/9, 2020 at 16:24 Comment(0)
S
5

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

plt.ticklabel_format(style='plain')
Silicate answered 6/5, 2021 at 17:38 Comment(0)
O
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)
Oscitancy answered 11/6, 2021 at 22:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.