73

I am using matplotlib together with latex labels for the axis, title and colorbar labels

While it works really great most of the time, it has some issues when you have a formula using \text.

One really simple example.

from matplotlib import pyplot as plt
plt.plot([1,2,3])
plt.title(r"$f_{\text{cor, r}}$")

plt.show()

This will result in an error message like:

IPython/core/formatters.py:239: FormatterWarning: Exception in image/png formatter: 
f_{\text{1cor, r}}
   ^
Unknown symbol: \text (at char 3), (line:1, col:4)
  FormatterWarning,

Is there an easy way to use \text in there?

Jakob
  • 19,815
  • 6
  • 75
  • 94
Daniel Wehner
  • 2,159
  • 1
  • 18
  • 22
  • [LATEX Mathematical Symbols](https://www.caam.rice.edu/~heinken/latex/symbols.pdf) & [matplotlib: Rendering math equations using TeX](https://matplotlib.org/stable/gallery/text_labels_and_annotations/tex_demo.html) – Trenton McKinney Aug 27 '21 at 00:30

2 Answers2

94

\text won't work because it requires the amsmath package (not included in mathtext - the math rendering engine of matplotlib). So you basically have two options:

  • use latex based font rendering
from matplotlib import pyplot as plt
import matplotlib as mpl
mpl.rcParams['text.usetex'] = True
mpl.rcParams['text.latex.preamble'] = [r'\usepackage{amsmath}'] #for \text command
plt.plot([1,2,3])
plt.title(r"$f_{\text{cor, r}}$")
plt.show()
  1. use mathtext but use \mathrm instead of \text
from matplotlib import pyplot as plt
import matplotlib as mpl
mpl.rcParams['text.usetex'] = False  # not really needed
plt.plot([1,2,3])
plt.title(r"$f_{\mathrm{cor, r}}$")
plt.show()

The latter approach creates a figure like enter image description here
Be aware that unlike with the \text command, spaces inside the \mathrm environment are not respected. If you want more space between the variables you have to use latex style commands (\<space>, \;, ...).

Jakob
  • 19,815
  • 6
  • 75
  • 94
  • 1
    Thank you! Especially for the actual explanation why it behaves like that. – Daniel Wehner May 25 '14 at 16:09
  • 1
    If you want to export your plot as pgf you also need mpl.rcParams['pgf.preamble'] = [r'\usepackage{amsmath}'] – Daniel Böckenhoff May 09 '19 at 15:51
  • The first method may turn out to screw some other standard `mathplotlib` features. For example, `ticklabel_format`'s `scilimit` uses `\mathdefault`, which my TeX Live does not recognize as a command. – foki Feb 25 '21 at 18:50
  • @DanielBöckenhoff Gnaaarrrrrr... or in cases like mine, when you uncomment `fig.savefig('name.pgf')` and the error pops up, you need both, `text.latex.preamble` and `pgf.preamble` set. E.g. add `plt.rcParams.update({ "text.latex.preamble": plt.rcParams["pgf.preamble"] })` You let me on the right track! Thank you! – Suuuehgi Aug 12 '22 at 09:58
3

One option is to let matplot lib use LaTeX directly for your text rendering (rather than the mathtext implementation that matplotlib provides).

import matplotlib as mpl
mpl.rcParams['text.usetex'] = True
# (create your plot as before)
plt.title(r"$f_{\mathrm{cor, r}}$")

This requires a fully working LaTeX installation. I can't seem to get it to recognise \text, but \mathrm does work (for 'roman family' font) just fine.

Bonlenfum
  • 19,101
  • 2
  • 53
  • 56
  • Sure, this solves the problem, though often I would like to keep normal rendering for the rest of it. – Daniel Wehner May 24 '14 at 09:36
  • 1
    yes, I agree (and generally try to avoid full LaTeX when plotting if I can). It seems that I was wrong about the `mathrm` environment requiring the `text.usetex` config setting - @Jakob's answer shows that it is implemented in mathtext. – Bonlenfum May 28 '14 at 10:51
  • `\mathrm` doesn't preserve spaces – Michael D Sep 10 '20 at 11:17