Prevent scientific notation

Question:

I have the following code:

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()

which produces the following plot:

plot

How do I prevent scientific notation here? Is ticklabel_format broken? does not resolve the issue of actually removing the offset.

plt.plot(np.arange(1e6, 3 * 1e7, 1e6))
plt.ticklabel_format(useOffset=False)

enter image description here

Asked By: Travis Osterman

||

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

Answered By: Joe Kington

Another way to prevent scientific notation is to "widen" the interval where scientific notation is not used using the scilimits= parameter.

plt.plot(np.arange(1e6, 3 * 1e7, 1e6))
plt.ticklabel_format(scilimits=(-5, 8))

result1

Here, scientific notation is used on an axis if the axis limit is less than 10^-5 or greater than 10^8.

By default, scientific notation is used for numbers smaller than 10^-5 or greater than 10^6, so if the highest value of the ticks are in this interval, scientific notation is not used.

So the plot created by

plt.plot(np.arange(50), np.logspace(0, 6));
plt.ylim((0, 1000000))

has scientific notation because 1000000=10^6 but the plot created by

plt.plot(np.arange(50), np.logspace(0, 6));
plt.ylim((0, 999999));

does not because the y-limit (999999) is smaller than 10^6, the default limit.

This default limit can be changed by using the scilimits= parameter of ticklabel_format(); simply pass a tuple of the format: (low, high) where the upper limit of the ticks should be in the interval (10^low, 10^high). For example, in the following code (a little extreme example), ticks are shown as full numbers because np.logspace(0,100)[-1] < 10**101 is True.

plt.plot(np.logspace(0, 8), np.logspace(0, 100));
plt.ticklabel_format(scilimits=(0, 101))

result2

Answered By: cottontail
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.