Annotations & Text

Matplotlib is a Python library for creating charts and visualizations — and annotations let you write labels directly on a chart and draw arrows pointing at the exact data points that matter.

Learn Annotations & Text in our free Matplotlib course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Matplotlib course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll add free-floating text with plt.text(), point arrows at a peak with ax.annotate(), and wrap labels in a styled box.

The simplest way to write on a chart is plt.text(x, y, "label") . The first two arguments are data coordinates — where on the chart the text begins — and the third is the string to show.

What you'll see: the rising curve with the words "x squared" sitting just beside the point (3, 9). The text floats at that data location with no arrow — that's the difference from annotate().

To draw an arrow from a label to a target point, use ax.annotate() . The xy argument is the point the arrow points to ; xytext is where the label sits; and arrowprops describes the arrow itself.

What you'll see: the zig-zag line with the text "Big jump" floating at (2.5, 32) and a thin arrow stretching from those words down to the high point at (4, 30) — drawing the eye straight to it.

A common use is highlighting the maximum . You can find the peak with Python's max() and feed its coordinates into annotate() .

What you'll see: the line chart with a red arrow pointing at the tallest dot (x=4, y=12) and the label "Max = 12" beside it — computed automatically from the data, not hard-coded.

To make a label stand out, draw a box behind it with the bbox argument. Pass a dictionary that sets the box shape ( boxstyle ), fill color ( facecolor ), and transparency ( alpha ).

What you'll see: the line chart with the word "Important!" sitting inside a soft, rounded, semi-transparent yellow box near (1, 4) — far more eye-catching than bare text.

Replace each ___ to point an arrow at the data point (3, 9).

You left out arrowprops . Add arrowprops=dict(arrowstyle="->") to draw the arrow.

xy and xytext use data coordinates. Pick values inside your axis ranges, not pixels.

Plot monthly visitors, then point a boxed arrow at the busiest month.

Lesson 13 complete — your charts can talk now!

You placed plain text with plt.text(), pointed arrows at a peak with ax.annotate(), and boxed a label with a bbox dictionary.

🚀 Up next: Plotting with Dates — put real calendar dates on the x-axis.

Practice quiz

What does plt.text(x, y, 'label') do?

  • Places plain text at a data coordinate
  • Draws an arrow
  • Adds a legend
  • Sets the axis limits

Answer: Places plain text at a data coordinate. plt.text places a plain string at the given data point with no arrow.

Which function can draw an arrow from a label to a target point?

  • plt.text()
  • ax.annotate()
  • ax.legend()
  • plt.grid()

Answer: ax.annotate(). ax.annotate() can draw an arrow using xy, xytext, and arrowprops.

In ax.annotate(), what does the xy argument specify?

  • The figure size
  • The text color
  • The point the arrow points TO
  • Where the text sits

Answer: The point the arrow points TO. xy is the data point being highlighted — where the arrow points.

In ax.annotate(), what does xytext specify?

  • The arrow style
  • The marker shape
  • The axis label
  • Where the text label sits

Answer: Where the text label sits. xytext is the location of the text label itself.

Which argument actually draws the arrow in annotate()?

  • arrowprops
  • xytext
  • bbox
  • fontsize

Answer: arrowprops. Without arrowprops, annotate places text but draws no arrow.

What units do xy and xytext use by default?

  • Pixels
  • Data coordinates
  • Inches
  • Points

Answer: Data coordinates. By default xy and xytext are in data coordinates of the axes.

How do you draw a box behind a text label?

  • color=
  • marker=
  • bbox=dict(...)
  • alpha=

Answer: bbox=dict(...). The bbox argument takes a dict with boxstyle, facecolor, and alpha.

What does arrowstyle='->' produce in arrowprops?

  • A filled rectangle
  • A dashed grid
  • No arrow
  • A simple line arrow with a head

Answer: A simple line arrow with a head. arrowstyle='->' gives a basic line arrow pointing at xy.

If you omit xytext in annotate(), what happens?

  • The text is placed at xy with no arrow
  • An error is raised
  • The arrow doubles in size
  • The text disappears

Answer: The text is placed at xy with no arrow. Without xytext the text sits right at xy and no arrow is drawn.

Why might a label land off the chart?

  • fontsize is too large
  • The arrow is missing
  • xy/xytext used values outside the axis ranges
  • alpha is set to 1

Answer: xy/xytext used values outside the axis ranges. xy and xytext are data coordinates; values outside the ranges place text off-chart.