You are reading an old version of the documentation (v2.2.2). For the latest version see
Version 2.2.2
Fork me on GitHub

Related Topics

Labeling ticks using engineering notationΒΆ

Use of the engineering Formatter.

import matplotlib.pyplot as plt
import numpy as np

from matplotlib.ticker import EngFormatter

# Fixing random state for reproducibility
prng = np.random.RandomState(19680801)

# Create artificial data to plot.
# The x data span over several decades to demonstrate several SI prefixes.
xs = np.logspace(1, 9, 100)
ys = (0.8 + 0.4 * prng.uniform(size=100)) * np.log10(xs)**2

# Figure width is doubled (2*6.4) to display nicely 2 subplots side by side.
fig, (ax0, ax1) = plt.subplots(nrows=2, figsize=(7, 9.6))
for ax in (ax0, ax1):

# Demo of the default settings, with a user-defined unit label.
ax0.set_title('Full unit ticklabels, w/ default precision & space separator')
formatter0 = EngFormatter(unit='Hz')
ax0.plot(xs, ys)

# Demo of the options `places` (number of digit after decimal point) and
# `sep` (separator between the number and the prefix/unit).
ax1.set_title('SI-prefix only ticklabels, 1-digit precision & '
              'thin space separator')
formatter1 = EngFormatter(places=1, sep=u"\N{THIN SPACE}")  # U+2009
ax1.plot(xs, ys)
ax1.set_xlabel('Frequency [Hz]')


Keywords: matplotlib code example, codex, python plot, pyplot Gallery generated by Sphinx-Gallery