October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Add Multiple Lines of Text to a Matplotlib Plot

Use newline characters in Matplotlib text strings to create multiline labels, notes, and annotations, then choose coordinates and alignment to suit the plot.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.Support on Ko-Fi

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.