# Plotting a latex matrix using text()

I would like to plot a latex'd matrix at a location in a 2d plot. I tried this:

text('$\\left( \\begin{array}{ll} 2 & 3 \\\\ 4 & 5 \\end{array} \\right)$', (1,0) )


and various variations to do with the backslashes being escaped or not. But every time I get an error:

matplotlib.pyparsing.ParseFatalException: Expected end of math '$'$\left( \begin{array}{ll} 2 & 3 \\ 4 & 5 \end{array} \right)$(at char 0), (line:1, col:1)  It appears that matplotlib doesn't want to parse an array or a pmatrix etc. Is there a workaround? edit retag close merge delete ## 1 answer Sort by » oldest newest most voted matplotlib uses its own typsetting system called mathtext by default. I think the problem you are getting is due to mathtext not supporting arrays in math mode? In any case, you can work around this by telling matplotlib to use your system LaTeX instead of mathtext like this: from matplotlib import rc rc('text', usetex=True) my_matrix = r'$\left( \begin{array}{ll} 2 & 3 \\ 4 & 5 \end{array} \right)\$'
text(my_matrix, (1,1))


You can look at another example here, it shows you how to change fonts to match the font in your document.

more

Thanks! This solved my problem.

( 2012-07-18 11:16:00 -0500 )edit