Text, Math & LaTeX in Plots

Annotations are the words, arrows, and equations you layer onto a chart to point out features and explain what the reader is looking at.

Learn Text, Math & LaTeX in Plots in our free Matplotlib course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a…

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 place text with ax.text() , draw labeled arrows with ax.annotate() , pin labels to corners with transAxes , and typeset equations using mathtext between dollar signs.

The simplest annotation is ax.text(x, y, "label") , which drops a string at the data coordinate you give. Style it with fontsize , color , and weight . Because the position is in data units, the text follows your data if the axes rescale.

What you'll see: a sine wave with the bold, dark-red phrase "Here is some text" floating near the top of the plot at x = 5, y = 0.8.

To point at a feature, use ax.annotate() . Give it the target point with xy , the label position with xytext , and an arrowprops dictionary to draw the connector. This is the standard way to call out a peak, an outlier, or a region of interest.

What you'll see: a sine wave with the word "Peak!" sitting just above and to the right of the first crest, connected to the exact peak point by a thin arrow.

Wrap an expression in dollar signs to render it as math, and use a raw string (the r"..." prefix) so backslash commands like \sqrt survive. To pin a label to a corner regardless of the data, pass transform=ax.transAxes , which switches to axes-fraction coordinates from (0, 0) at the bottom-left to (1, 1) at the top-right.

What you'll see: a purple bell curve titled with the rendered equation f(x) = e to the minus x squared, axis labels in italic math, and a rounded wheat-colored box in the top-left holding the Gaussian integral that equals the square root of pi.

Replace each ___ to pin a label to the top-left corner using axes coordinates.

Hint: the axes-fraction transform is ax.transAxes .

You forgot the raw-string prefix. Use r"$\sqrt{x}

quot; so Python doesn't eat the backslash before Matplotlib renders it.

You placed it in data coordinates. Add transform=ax.transAxes to pin it to a fixed spot on the Axes instead.

An arrow only shows when you pass arrowprops . Without it, annotate behaves like plain text.

Plot a damped oscillation with a mathtext title, an arrow pointing at the first trough, and a boxed mathtext note pinned to a corner.

Lesson complete — your charts can speak for themselves!

You placed text with ax.text() , drew labeled arrows with ax.annotate() , pinned labels using transAxes , and typeset equations with mathtext between dollar signs.

🚀 Up next: Animations (FuncAnimation) — bring your plots to life frame by frame.

Practice quiz

Which call drops a plain string at a data coordinate?

  • ax.label(x, y, 'hi')
  • ax.put(x, y, 'hi')
  • ax.text(x, y, 'hi')
  • ax.write(x, y, 'hi')

Answer: ax.text(x, y, 'hi'). ax.text(x, y, 'label') places a plain string at the given data coordinate.

What is ax.annotate() specifically built for?

  • Saving figures
  • Resizing axes
  • Setting the colormap
  • Callouts with an arrow pointing at a feature

Answer: Callouts with an arrow pointing at a feature. annotate() takes xy, xytext, and arrowprops to draw a labeled arrow.

In ax.annotate(), which argument is the point being marked?

  • xy
  • xytext
  • point
  • target

Answer: xy. xy is the point to mark; xytext is where the label text sits.

What makes annotate() draw an arrow rather than plain text?

  • color=
  • arrowprops=
  • marker=
  • linestyle=

Answer: arrowprops=. An arrow only appears when you pass an arrowprops dictionary.

How do you switch into Matplotlib's math rendering (mathtext)?

  • Wrap text in #...#
  • Use math=True
  • Wrap the expression in dollar signs $...$
  • Use ax.math()

Answer: Wrap the expression in dollar signs $...$. Mathtext is enabled by wrapping the expression in dollar signs, e.g. $E=mc^2$.

Why use a raw string r'...' for a mathtext label?

  • It centers the text
  • It makes the font bold
  • It enables LaTeX install
  • So backslash commands like \sqrt are passed through literally

Answer: So backslash commands like \sqrt are passed through literally. A raw string keeps backslashes intact so commands like \alpha survive.

What does transform=ax.transAxes switch the coordinates to?

  • Axes-fraction coordinates from (0,0) to (1,1)
  • Pixel coordinates
  • Polar coordinates
  • Inches from the origin

Answer: Axes-fraction coordinates from (0,0) to (1,1). transAxes uses axes-fraction coordinates, with (0,0) bottom-left and (1,1) top-right.

Which dictionary draws a box around text?

  • box=dict(...)
  • bbox=dict(boxstyle='round', facecolor='wheat')
  • frame=dict(...)
  • border=dict(...)

Answer: bbox=dict(boxstyle='round', facecolor='wheat'). Pass a bbox dictionary with boxstyle, facecolor, and alpha to box the text.

In mathtext, how do you write a superscript like x squared?

  • x*2
  • x#2
  • x^2
  • x~2

Answer: x^2. Superscripts use ^ and subscripts use _ in mathtext, with braces to group.

Is a LaTeX installation required to use mathtext?

  • No — mathtext works out of the box
  • Yes, always
  • Only on Windows
  • Only for subscripts

Answer: No — mathtext works out of the box. Mathtext is built in and needs no external LaTeX install.