The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Put newline characters (n) in the string passed to a Matplotlib text method. For example, ax.text(0.5, 0.5, "First linenSecond linenThird line") draws a three-line text block. By default, the coordinates are in the plot’s data space; use transform=ax.transAxes when you want to position the block relative to the Axes instead.
Make a multiline text block with ax.text
Use a newline character wherever a new line should begin. This example places a three-line block at data coordinates x=0.5 and y=0.5:
ax.text(0.5, 0.5, "First linenSecond linenThird line")
Matplotlib’s multiline text example confirms that text strings can contain newlines. The same approach works for titles and axis labels; the example also demonstrates multiline x- and y-labels.
Choose coordinates that fit the note
Use data coordinates for a location in the plot
ax.text(x, y, text) uses data coordinates by default. The position therefore refers to values on the plotted x- and y-axes, and the text moves in relation to the data when limits or plotted values change. See the text API reference.
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware match#1 Best Overall
Use Axes coordinates for a fixed corner note
Pass transform=ax.transAxes to locate text relative to the Axes rectangle. In this coordinate system, (0, 0) is the lower-left corner and (1, 1) is the upper-right. This is useful for a note that should stay in a corner as the data limits change.
ax.text(
0.02, 0.98,
"Peak: 8.2nMean: 4.1",
transform=ax.transAxes,
ha="left", va="top",
bbox={"facecolor": "white", "alpha": 0.8, "edgecolor": "none"},
)
The bbox argument accepts a dictionary of rectangle properties, so a lightly opaque background can help the text remain legible over plotted data.
Rank #2
Use ax.annotate when the text explains a point
For a note tied to a particular plotted point, use ax.annotate(text, xy=point). The xy argument identifies the point; xytext can put the text elsewhere, and arrowprops can draw a connecting arrow. See the Axes annotation reference.
Align and style the lines
ha (or horizontalalignment) and va (or verticalalignment) position the text block relative to its anchor. For alignment within the block, set multialignment to "left", "center", or "right". If omitted, Matplotlib infers line alignment from the horizontal and vertical alignment settings. linespacing adjusts the vertical spacing between lines.
ax.text(
0.5, 0.5,
"HeadingnFirst detailnSecond detail",
ha="left",
va="top",
multialignment="left",
fontsize=12,
linespacing=1.2,
)
These properties are described in the Matplotlib multiline example and Text properties reference.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Leave room for multiline labels
Long text can run into the edge of a figure or overlap nearby content. Matplotlib’s multiline example recommends a layout manager such as layout="constrained" to make room for multiline labels. Check the rendered figure as well: depending on the text and figure dimensions, you may still need to adjust the figure size or layout.
import matplotlib.pyplot as plt
fig, ax = plt.subplots(layout="constrained")
ax.plot([1, 2, 3], [2, 4, 3])
ax.text(2, 4, "Local maximumnCheck second series", ha="center")
Here the label uses data coordinates, so it is attached to the location (2, 4). If it should instead remain in a fixed Axes corner, use transform=ax.transAxes and choose a position between 0 and 1 for each coordinate.
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.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.




