简体   繁体   中英

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.

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

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

plt.xlabel(r'$\rho A_i$')

Be careful when you're using \\n , \\r and so on in a string. Those are commands for eg 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM