HOWTO · Matplotlib

How to Set Tick Label in Scientific Notation in Matplotlib

This article teaches you how to set tick labels in scientific notation using Matplotlib in Python. Learn about the `ticklabel_format()` function, `ScalarFormatter`, and `FuncFormatter` to enhance your data visualizations. Perfect for beginners and experienced users alike, this guide provides clear examples and explanations to help you create professional-looking graphs.

On this page

To display scientific-notation tick labels in Matplotlib, call ticklabel_format(style="sci", scilimits=(0, 0)) on the axes you want to format. The example below applies it to the y-axis of a labeled plot; use axis="both" to format both axes.

Format tick labels with ticklabel_format()

Run this example:

import logging

# The first font-cache build is a normal startup notice, not an execution error.
logging.getLogger("matplotlib.font_manager").setLevel(logging.ERROR)

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
x = np.linspace(0, 1_000_000, 100)
y = 22_000 * (x / 1_000_000) ** 10
ax.plot(x, y)
ax.set_xlabel("X values")
ax.set_ylabel("Y values")
ax.set_title("Scientific Notation in Matplotlib")
ax.ticklabel_format(axis="y", style="sci", scilimits=(0, 0))
fig.tight_layout()
plt.show()
print("Scientific notation enabled on the y-axis.")
Scientific notation enabled on the y-axis.

A labeled Matplotlib line plot with scientific-notation tick labels on the y-axis

plt.show() opens the labeled plot in an interactive Matplotlib environment. The plot visual above shows the same title, labels, axis ranges, and rapidly rising curve; the code and printed status remain selectable.

ticklabel_format() is a convenient pyplot method for configuring the scalar formatter already used by an axis. The style value is "sci" for scientific notation or "plain" for ordinary decimal labels. The valid axis choices are "x", "y", and "both"; the argument is named axis, singular. In an object-oriented plot, call ax.ticklabel_format(...) on the relevant Axes object.

Choose the axis and when scientific notation appears

Use axis="x" or axis="y" when only one set of ticks needs a different display, or axis="both" when both do. The default scilimits setting lets Matplotlib choose when to switch to scientific notation. The tuple’s integers are exponent thresholds applied to the axis range, not numeric-value bounds: they guide when the scalar formatter switches between plain and scientific labels. Setting scilimits=(0, 0) is the special case that forces scientific notation for scalar tick labels, which is useful when a plot should use that form consistently. This changes label formatting only; it does not change the underlying data or tick locations.

The example explicitly formats only the y-axis to demonstrate the selected axis; Matplotlib may independently choose scientific notation for the large x values. If you format both axes, both sets of ticks use the requested scalar formatting policy. Choose the axis based on the values and the comparison the reader needs to make.

Customize scientific notation with ScalarFormatter

Use ScalarFormatter when you need to configure the formatter directly, including math-text exponent styling or power limits. Create one with ScalarFormatter(useMathText=True), then attach it with ax.yaxis.set_major_formatter(formatter). For both axes, configure each axis separately. To control the range, set formatter.set_powerlimits((m, n)); formatter.set_scientific(True) explicitly enables scientific notation. This is the standard scalar formatter behind ticklabel_format(), so it is a natural choice for conventional numeric ticks with more control over exponent presentation.

Define custom tick labels with FuncFormatter

Use FuncFormatter when the label rule itself must be custom, such as adding units or choosing a fixed number of significant digits. Its callback receives the tick value and its position, conventionally as def format_tick(value, position): ..., and returns the label as a string. For example, FuncFormatter(lambda value, position: f"{value:.2e}") formats each value in scientific notation; attach it to the y-axis with ax.yaxis.set_major_formatter(...). The position argument can be ignored when the label depends only on the value. Unlike ScalarFormatter, this callback defines the complete label text, so it does not automatically coordinate a shared exponent or math-text styling.

Troubleshoot formatter behavior

If ticklabel_format() raises an AttributeError, inspect the axis formatter: the method configures a ScalarFormatter and does not apply to every formatter class. This can happen after assigning a FuncFormatter or a date/log formatter. Call ticklabel_format() before replacing the formatter, or use ScalarFormatter directly when you need to restore scalar formatting. Do not assume that all formatter classes accept the same options.

On a logarithmic axis, the scale and its formatter determine tick locations and labels. A scalar formatter configuration that works on a linear axis is not a universal log-axis recipe; use the scale-appropriate formatter unless you have a specific reason to replace it, and inspect the resulting ticks. If labels show an unexpected exponent, verify the selected axis, its scale, and the formatter currently attached before adjusting scilimits.