Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prevent scientific notation in matplotlib.pyplot [duplicate]

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

like image 509
Travis Osterman Avatar asked Feb 06 '15 17:02

Travis Osterman


People also ask

How do I stop matplotlib from scientific notation?

To prevent scientific notation, we must pass style='plain' in the ticklabel_format method.

How do you prevent numbers being changed to exponential form in python matplotlib figure?

How to prevent numbers being changed to exponential form in Python Matplotlib? Using style='plain' in the ticklabel_format() method, we can restrict the value being changed into exponential form.

How do I get rid of scientific notation in Python?

Summary: Use the string literal syntax f"{number:. nf}" to suppress the scientific notation of a number to its floating-point representation.

What is Pyplot CLF ()?

The clf() function in pyplot module of matplotlib library is used to clear the current figure.


1 Answers

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

like image 120
Joe Kington Avatar answered Sep 23 '22 00:09

Joe Kington