Free tools Windows power users keep installed
One-click scans. No signup required.
Use ax.text() with transform=ax.transAxes to place text relative to an Axes rather than at a data value. Axes coordinates run from (0, 0) at the lower-left to (1, 1) at the upper-right, so (0.5, 0.5) is the center.
Place text in a corner or the center of an Axes
This example adds an upper-left label and centered text to the same plot:
As an Amazon Associate I earn from qualifying purchases.
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([0, 1, 2], [0, 1, 0])
# Upper-left, inset slightly from the Axes boundary.
ax.text(
0.02, 0.98, "Upper-left label",
transform=ax.transAxes,
ha="left", va="top",
)
# Center of the Axes.
ax.text(
0.5, 0.5, "Centered label",
transform=ax.transAxes,
ha="center", va="center",
)
plt.show()
The 0.02 and 0.98 values inset the corner label slightly from the edges. For the other corners, use an inset coordinate and match the alignment to that corner:
- Upper right:
x=0.98, y=0.98, ha="right", va="top" - Lower left:
x=0.02, y=0.02, ha="left", va="bottom" - Lower right:
x=0.98, y=0.02, ha="right", va="bottom"
These positions stay relative to the Axes rectangle when plotted data or axis limits change. See Matplotlib’s text properties and layout documentation.
#1 Best Overall
Why the coordinate system matters
ax.text() and plt.text() use data coordinates by default. In that frame, a position such as (0.5, 0.5) means the data point x=0.5, y=0.5; it does not necessarily mean the visual center. A label placed at a data value moves on screen as the limits or data change.
Set transform=ax.transAxes when the location should track the plotting rectangle instead. This transform maps the Axes fractions (0, 0) and (1, 1) to its lower-left and upper-right corners. The examples use ax.text(), which is convenient when working with an Axes object; plt.text() can also be used with the appropriate transform. Matplotlib documents the behavior in its text API reference.
Rank #2
Align the text box to its anchor
The coordinates specify an anchor point, and alignment determines which part of the text box sits there. ha (horizontal alignment) accepts values such as "left", "center", or "right"; va (vertical alignment) accepts "bottom", "center", or "top". For example, ha="right", va="top" makes the text box’s upper-right meet its anchor. Explicit alignment avoids relying on the default left and baseline alignment. See Matplotlib’s alignment guidance.
Choose between Axes, figure, and data placement
| Method and frame | Follows data limits? | Points to a data target? | Best for |
|---|---|---|---|
ax.text() with ax.transAxes: Axes rectangle |
No; position stays relative to the Axes. | No | Corner or center overlays inside one plot. |
fig.text(): whole figure coordinates, from 0 to 1 by default |
No; position is relative to the figure. | No | Text positioned across the figure rather than within a particular Axes. See the Figure.text API. |
ax.text() in its default data coordinates |
Yes; the location is a data value. | It can label a value, but has no separate target and text positions. | Text tied to a location in data space. |
ax.annotate(): target and text can use coordinate systems such as data, Axes fraction, or figure fraction |
Depends on the coordinate system chosen for the target and text. | Yes; target and text locations can be separate, with an optional arrow. | Explanatory labels linked to a point. See Matplotlib’s text API reference. |
When to use annotate instead
Use ax.annotate() when the label explains a particular point, especially if you want an arrow or need the target and displayed text in different places. Use Axes-relative ax.text() for a fixed corner or center label that is not tied to a specific data point.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




