2025, Nov 12 17:00

How to Make Bold Text in Matplotlib Annotations: Avoid the \textbf Error and Use \mathbf Instead

Learn why Matplotlib annotations fail with a \textbf ParseSyntaxException and how to bold math text correctly using \mathbf. Simple, reliable fix with code.

Making text bold inside Matplotlib annotations can be surprisingly tricky when you rely on LaTeX-like syntax. A common pattern people try is to wrap the content with \textbf, only to be greeted by a parser error that looks cryptic at first glance. Let’s unpack what’s going on and show a minimal, reliable way to bolden text in this scenario.

Problem setup

The goal is to render a bold annotation on a plot. The attempt below uses a LaTeX-style command inside the annotated string:

import matplotlib.pyplot as plt

canvas, axes_obj = plt.subplots(figsize=(6, 4))

axes_obj.annotate(r"$ \textbf {y = -\,\frac{1}{\sqrt{2}}x + 1} $", 
                  xy=(0.2, 0.7), rotation=-35, fontweight='bold', fontsize=12)

plt.show()

Instead of bold text, the code triggers an error:

ValueError:
\textbf {y = -\,\frac{1}{\sqrt{2}}x + 1}
     ^
ParseSyntaxException: Expected \text, found 'bf' (at char 6), (line:1, col:7)

What the error really says

The message is actually quite direct. The parser interprets the token sequence as \text followed by the separate text bf. In other words, \textbf is not recognized as a single valid command in this environment; it splits into \text and the following fragment bf, which then fails with “Expected \text, found 'bf'.” If you also encountered references to \txtbf, that’s just a typo; correcting it to \textbf still leads to the same issue here.

Fix: use \mathbf in the annotation string

In this setup, there is no \textbf available, which is why the parser raises the error. Switching to \mathbf resolves the problem and produces bold rendering in the annotation:

import matplotlib.pyplot as plt

canvas, axes_obj = plt.subplots(figsize=(6, 4))

axes_obj.annotate(r"$ \mathbf {y = -\,\frac{1}{\sqrt{2}}x + 1} $", 
                  xy=(0.2, 0.7), rotation=-35, fontweight='bold', fontsize=12)

plt.show()

This change keeps the same visual intent—boldening the formula content—without triggering the parser error.

Why this matters

Annotation pipelines often mix plotting code with inline math markup. When the parser doesn’t recognize a macro, it can split tokens in unexpected ways and fail with messages that look unrelated to your intention. Knowing which commands are accepted avoids time lost debugging formatting rather than focusing on the data and visuals. In this particular case, switching from \textbf to \mathbf is enough to get bold math content rendered as expected.

Takeaways

If you’re bolding text in Matplotlib annotations and hit a syntax error that points at \textbf—especially one saying “Expected \text, found 'bf'”—use \mathbf inside the math string instead. Also double-check for any \txtbf vs \textbf mix-ups. With these adjustments, your annotations render cleanly and your plotting code stays straightforward.