Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib LaTeX: Inconsistent Behaviour with Greek Letters (Specifically \rho)

I'm trying to add some axis-labels to a graph which contains the Greek letter 'rho'. To do this I want to use the LaTeX capability of Matplotlib but it seems to have a problem with the \rho symbol.

Here is a minimal example:

import matplotlib.pyplot as plt
from matplotlib import rc,rcParams

rc('text',usetex=True)
rcParams.update({'font.size': 16})

plt.plot([0,1,2,3,4],[0,1,4,9,16])
plt.xlabel('\rho A_i') # works if \rho is replaced with, for example, \sigma
plt.ylabel('Something else')
plt.show()

Upon running the first time I get a bunch of LaTeX errors and a blank figure window, running again shows the graph but the xlabel reads 'ho Ai ' where the i is subscript as expected.

The weird thing is if I replace \rho with something else, say, \sigma it shows up correctly. Can anybody tell me why it is not happy with my code example and how to fix it?

Thanks.

P.s. I tried putting the expression in $..$ but that changed nothing.

like image 668
Dan Avatar asked Aug 14 '12 17:08

Dan


2 Answers

I think you are supposed to use raw strings, and use the $ signs as well. Try:

plt.xlabel(r'$\rho A_i$')
like image 135
reptilicus Avatar answered Nov 15 '22 19:11

reptilicus


Be careful when you're using \n , \r and so on in a string. Those are commands for e.g. entering a new line etc.

https://docs.python.org/2/library/re.html

To make sure you don't use these regular expression operators put \\rho instead of \rho.

like image 39
MissSophie Avatar answered Nov 15 '22 20:11

MissSophie