5 Best Ways to Prevent Numbers Being Changed to Exponential Form in Python Matplotlib

Rate this post

πŸ’‘ Problem Formulation: When plotting large or small numerical values using Python’s Matplotlib, the axis tick labels often default to exponential notation. This can be less readable and might not be desired for all user cases. For example, if the data points range into the millions, Matplotlib may display the axis labels in scientific notation like “1e6” instead of “1000000”. This article explores how to maintain standard numeric formatting easily.

Method 1: Use ticklabel_format Style

The ticklabel_format function of the axis can be used to prevent numbers from being changed to exponential form. This method allows you to specify the style of the tick labels, which can be set to ‘plain’ to avoid scientific notation.

Here’s an example:

import matplotlib.pyplot as plt

plt.figure()
plt.plot(range(1000000))
plt.ticklabel_format(style='plain', axis='y')
plt.show()

The output shows a plot with the y-axis labeled in plain numbers rather than exponential form.

This method is straightforward and directly tells the Matplotlib axis how to format the tick labels, switching off scientific notation where it’s not wanted.

Method 2: ScalarFormatter

The ScalarFormatter from the Matplotlib ticker module can prevent scientific notation in tick labels. It uses a given format string to display tick labels.

Here’s an example:

import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter

ax = plt.gca()
formatter = ScalarFormatter(useOffset=False)
formatter.set_scientific(False)
ax.yaxis.set_major_formatter(formatter)

plt.plot(range(1000000))
plt.show()

The output will be a plot with a y-axis displaying labels as plain numbers.

This method provides a fine-grained control over tick formatting, and the ScalarFormatter can be used for more complex tick label customizations as well.

Method 3: Manual Tick Setting

Manually setting the ticks using set_xticks and set_yticks allows for complete control over the display of axis labels. This approach is particularly useful when you have specific tick labels in mind.

Here’s an example:

import matplotlib.pyplot as plt

plt.plot(range(1000000))
plt.gca().set_yticks([0, 250000, 500000, 750000, 1000000])
plt.gca().get_yaxis().get_major_formatter().set_useOffset(False)
plt.show()

The plot’s y-axis will show the tick labels specified in the set_yticks method.

While this method gives precise control over the axis ticks, it is less flexible and requires manual updating if the data changes significantly.

Method 4: Update Default rcParams

By updating Matplotlib’s default runtime configuration (rcParams), you can set the default axis formatting for all plots in your session, overriding the default behavior of using exponential notation.

Here’s an example:

import matplotlib.pyplot as plt

plt.rcParams['axes.formatter.useoffset'] = False
plt.rcParams['axes.formatter.sci_limit'] = (-1, 1)

plt.plot(range(1000000))
plt.show()

The output will display the plot with y-axis tick labels in a plain numeric format.

This method is useful when you want to set the configuration at the beginning and apply it to all subsequent plots, ensuring consistency across your project.

Bonus One-Liner Method 5: Lambda Formatter

For a quick-and-dirty approach, a lambda function can be used to format the axis ticks inline without the need for external formatters.

Here’s an example:

import matplotlib.pyplot as plt

plt.plot(range(1000000))
plt.gca().yaxis.set_major_formatter(plt.FuncFormatter(lambda x, _: f'{x:.0f}'))
plt.show()

The plot will render with the y-axis tick labels in plain numbers, thanks to the custom lambda function formatter.

This method provides a simple one-liner solution for ad-hoc plotting but may not be ideal for complex formatting requirements.

Summary/Discussion

  • Method 1: Use ticklabel_format Style. Simple and direct. Lacks flexibility for complex scenarios.
  • Method 2: ScalarFormatter. Offers precise control. May seem overly detailed for simple plots.
  • Method 3: Manual Tick Setting. High precision. Impractical for dynamically changing datasets.
  • Method 4: Update Default rcParams. Convenient for setting application-wide defaults. May hide desirable exponential notation in some cases.
  • Bonus Method 5: Lambda Formatter. Quick and easy. Limited in terms of functionality and customizability.