First time here? Check out the FAQ!

Ask Your Question
3

Plotting a latex matrix using text()

asked 12 years ago

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?

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
4

answered 12 years ago

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.

Preview: (hide)
link

Comments

Thanks! This solved my problem.

Kate Stange gravatar imageKate Stange ( 12 years ago )

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: 12 years ago

Seen: 2,533 times

Last updated: Jul 14 '12