ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 18 Jul 2012 18:16:00 +0200Plotting a latex matrix using text()https://ask.sagemath.org/question/9155/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?Sat, 14 Jul 2012 09:04:17 +0200https://ask.sagemath.org/question/9155/plotting-a-latex-matrix-using-text/Answer by benjaminfjones for <p>I would like to plot a latex'd matrix at a location in a 2d plot. I tried this:</p>
<pre><code>text('$\\left( \\begin{array}{ll} 2 & 3 \\\\ 4 & 5 \\end{array} \\right)$', (1,0) )
</code></pre>
<p>and various variations to do with the backslashes being escaped or not. But every time I get an error:</p>
<pre><code>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)
</code></pre>
<p>It appears that matplotlib doesn't want to parse an array or a pmatrix etc. Is there a workaround?</p>
https://ask.sagemath.org/question/9155/plotting-a-latex-matrix-using-text/?answer=13821#post-id-13821matplotlib uses its own typsetting system called [mathtext](http://matplotlib.sourceforge.net/users/mathtext.html#mathtext-tutorial) 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](http://matplotlib.sourceforge.net/users/usetex.html), it shows you how to change fonts to match the font in your document.Sat, 14 Jul 2012 17:16:10 +0200https://ask.sagemath.org/question/9155/plotting-a-latex-matrix-using-text/?answer=13821#post-id-13821Comment by Kate Stange for <p>matplotlib uses its own typsetting system called <a href="http://matplotlib.sourceforge.net/users/mathtext.html#mathtext-tutorial">mathtext</a> by default. I think the problem you are getting is due to mathtext not supporting arrays in math mode? </p>
<p>In any case, you can work around this by telling matplotlib to use your system LaTeX instead of mathtext like this:</p>
<pre><code>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))
</code></pre>
<p>You can look at another example <a href="http://matplotlib.sourceforge.net/users/usetex.html">here</a>, it shows you how to change fonts to match the font in your document.</p>
https://ask.sagemath.org/question/9155/plotting-a-latex-matrix-using-text/?comment=19384#post-id-19384Thanks! This solved my problem.Wed, 18 Jul 2012 18:16:00 +0200https://ask.sagemath.org/question/9155/plotting-a-latex-matrix-using-text/?comment=19384#post-id-19384