Ask Your Question
3

Plotting a latex matrix using text()

asked 2012-07-14 09:04:17 +0100

Kate Stange gravatar image

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 flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
4

answered 2012-07-14 17:16:10 +0100

benjaminfjones gravatar image

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.

edit flag offensive delete link more

Comments

Thanks! This solved my problem.

Kate Stange gravatar imageKate Stange ( 2012-07-18 18:16:00 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-07-14 09:04:17 +0100

Seen: 2,478 times

Last updated: Jul 14 '12