HOWTO · Matplotlib

How to Add Text Inside the Plot in Matplotlib

Learn how to use matplotlib.pyplot.text() to place labels at data coordinates, set axis limits, customize text, and add annotations or multi-line notes to Matplotlib plots.

On this page

Use matplotlib.pyplot.text() to put a label or note at a chosen position inside a Matplotlib plot. It takes x and y coordinates followed by the string to display; optional arguments control the text’s appearance. For example, plt.text(2, 20, "Peak point") places a label at data coordinate (2, 20). With an axes object, use the equivalent ax.text(2, 20, "Peak point") call. See the pyplot.text() API reference for the full parameter list.

Use matplotlib.pyplot.text() to add text to a plot

The following example draws a line and places a label at one of its data points. It uses the pyplot interface and sets the displayed range so the chosen coordinates are included in the plot.

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [10, 15, 25, 30]

plt.plot(x, y, marker="o")
plt.xlim(0, 5)
plt.ylim(0, 35)
plt.text(3, 25, "Peak point", fontsize=12, color="crimson")
plt.title("A labeled line plot")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()

x and y hold the plotted data, and the call places “Peak point” at coordinate (3, 25). The two limit calls include that coordinate in the visible range. For scripts that create several plots, ax.text(x, y, label) attaches text to a specific axes and makes it clear which plot receives it. The pyplot call is convenient for a single current plot.

Position text with data coordinates and axis limits

By default, the x and y arguments to text() use data coordinates. A label at (3, 25) therefore follows those values on the graph; it is not positioned by pixel distance from a corner. The current x and y limits determine which part of the data-coordinate plane is visible. If the requested position falls outside those limits, the text is outside the visible plotting area. Set suitable limits with plt.xlim() and plt.ylim(), or use ax.set_xlim() and ax.set_ylim() with an axes object.

Choose a position that leaves the label readable and does not cover important data. For a note that should stay at a fixed fraction of the axes regardless of data values, use axes coordinates through transform=ax.transAxes; (0, 0) is the lower-left and (1, 1) the upper-right of the axes. This differs from the default data coordinates and is useful for captions anchored inside a plot.

Customize text appearance

Pass keyword arguments to text() to style a label. fontsize changes its size, color sets its color, and fontweight="bold" emphasizes it. Set rotation to turn the text, and use horizontalalignment (or ha) and verticalalignment (or va) to align it around its x and y coordinates. For example, ha="center" and va="bottom" center a label over a point while keeping it above that point.

A bbox argument adds a background behind the text, which can separate a label from a busy line or marker. It accepts a dictionary of patch properties. This compact example combines alignment, emphasis, rotation, and a translucent white box:

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1, 2, 3], [2, 4, 3], marker="o")
ax.text(2, 4, "Peak", fontsize=12, color="crimson", fontweight="bold",
        ha="center", va="bottom", rotation=15,
        bbox={"facecolor": "white", "alpha": 0.8})
plt.close(fig)

For instance, bbox={"facecolor": "white", "alpha": 0.8} creates a partly transparent white background. Use only options that improve legibility; strong colors, large fonts, or rotation can compete with the data when applied broadly.

Add annotations and multi-line text

Use plt.annotate() when a note needs to point to a feature. Its xy argument identifies the data point being described, while xytext gives the text position. For example, plt.annotate("Peak", xy=(3, 25), xytext=(2, 30), arrowprops={"arrowstyle": "->"}) places a note above the point and draws an arrow toward it. The annotation text position also uses data coordinates by default; set limits that include both the target and the note. With axes, use ax.annotate() so the target and note belong to the intended plot.

For a note on several lines, include newline characters in the string passed to text(), such as "First line\nSecond line". Matplotlib renders each segment on its own line at the selected position. A multi-line label is useful for a compact explanation; adjust its position and alignment so it remains within the visible plot area and does not obscure nearby data. These techniques let you add labels, context, and pointers while keeping each note tied to the coordinates or feature it describes.