0

I know there is a way to TeX inside the legend, but it doesn't understand arguments such as a line break or the matrix environment. I haven't been able to find any other resources that address drawing out a small matrix inside the legend itself, or even overlaying the matrix onto the plot. This is the best I can get: Plot

with this code:

plt.plot(perturbation, errors,label=r'$A=[ 3  2 ][ 4  3 ]$')
plt.legend(loc='upper left', prop={'size': 12})

Thanks for any suggestions!

  • 1
    https://matplotlib.org/stable/gallery/text_labels_and_annotations/tex_demo.html — in short, `matplotlib.rcParams['text.usetex'] = True` – gboffi Apr 23 '21 at 19:25
  • If I were to plot a fraction this would work fine, but it doesn't understand the matrix environment still. – liveFreeOrπHard Apr 23 '21 at 19:32

2 Answers2

4

Matplotlib can use a LaTex engine, but you need to have it installed on your computer and set:

plt.rcParams['text.usetex'] = True

See here: https://matplotlib.org/stable/tutorials/text/usetex.html


Without installing LaTeX

The default is to use mathtext, a limited set of latex. Mathtext has not built-in matrix commands, but you can approximate it using \genfrac{}{}{}{}{}{}. You do have to handle the whitespacing yourself. Example:

import numpy as np
from matplotlib import pyplot as plt

plt.ion()

x = np.linspace(0, 2 * np.pi, 300)
y = np.sin(x)

label = r'$A=\genfrac{[}{]}{0}{3}{\mathtt{\,3\;2}}{\mathtt{\,4\;3}}$'

plt.plot(x, y, label=label)
plt.legend(loc='lower left', prop={'size': 16})

Produces this plot: enter image description here

James
  • 32,991
  • 4
  • 47
  • 70
  • This looks awesome! When I try the same though I get the error: RuntimeError: latex was not able to process the following string: b'$A=\\\\genfrac{[}{]}{0}{3}{\\\\mathtt{\\\\,3\\\\;2}}{\\\\mathtt{\\\\,4\\\\;3}}$' – liveFreeOrπHard Apr 23 '21 at 21:18
  • 1
    Make sure you leave `plt.rcParams['text.usetex'] = False`; i.e., don't change it from the default. – James Apr 25 '21 at 20:58
2

For me it works

In [7]: import numpy as np
   ...: import matplotlib
   ...: matplotlib.rcParams['text.usetex'] = True
   ...: import matplotlib.pyplot as plt
   ...: t = np.linspace(0.0, 1.0, 100)
   ...: s = np.cos(4 * np.pi * t) + 2
   ...: 
   ...: fig, ax = plt.subplots(figsize=(6, 4), tight_layout=True)
   ...: ax.plot(t, s)
   ...: 
   ...: ax.set_xlabel(r'$\left[\matrix{ 1 & 0 \cr 0 & 1 }\right]$')
Out[7]: Text(0.5, 0, '$\\left[\\matrix{ 1 & 0 \\cr 0 & 1 }\\right]$')

enter image description here

gboffi
  • 22,939
  • 8
  • 54
  • 85
  • I actually got it to work this way, thanks a bunch! Maybe it's because I was using the bmatrix environment? – liveFreeOrπHard Apr 23 '21 at 21:20
  • 1
    @liveFreeOrπHard the `\matrix` macro is defined in LaTeX (no need for additional packages) for compatibility with plain TeX, on the other hand Matplotlib has a mechanism to define a LaTeX preamble (see, e.g., this [simple and to the point example](https://stackoverflow.com/a/41453758/2749397)) . – gboffi Apr 24 '21 at 08:04
  • 2
    @liveFreeOrπHard Note that, should you load the AMS math package, `matrix` is redefined as an environment and using `\matrix{...}` as a command raises an error, as it's detailed in [this answer](https://tex.stackexchange.com/a/468390/74057). – gboffi Apr 24 '21 at 08:15