Annotate points and format ticks

3.11.2

Highlight a meaningful point and control labels without changing the plotted values.

On this page

Point to a meaningful observation

annotate separates the data point xy from the text position xytext. Use offset points for short labels that should remain near a point when the axis limits change.

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

fig, ax = plt.subplots()
ax.plot([1, 2, 3], [0.18, 0.22, 0.20], marker="o")
ax.annotate("Peak", xy=(2, 0.22), xytext=(10, -14), textcoords="offset points")
ax.yaxis.set_major_formatter(PercentFormatter(xmax=1))
ax.set(xlabel="Week", ylabel="Conversion")
plt.show()
Conversion peaks at 22 percent in week two. The second point is labeled Peak. Download image

Separate tick positions from formatting

A tick locator chooses positions. A formatter turns each value into display text. For proportions stored from zero to one, PercentFormatter(xmax=1) displays 0.22 as 22 percent.

Official references