简体   繁体   English

Matplotlib LaTeX:行为与希腊字母不一致(特别是\\ rho)

[英]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'. 我正在尝试向包含希腊字母“ rho”的图形添加一些轴标签。 To do this I want to use the LaTeX capability of Matplotlib but it seems to have a problem with the \\rho symbol. 为此,我想使用Matplotlib的LaTeX功能,但是\\ rho符号似乎有问题。

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. 第一次运行时,我遇到一堆LaTeX错误和一个空白的图形窗口,再次运行显示该图,但xlabel读取' ho Ai ',其中i是预期的下标。

The weird thing is if I replace \\rho with something else, say, \\sigma it shows up correctly. 奇怪的是,如果我将\\rho替换为其他内容,例如\\sigma它将正确显示。 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. 在字符串中使用\\n\\r等时,请小心。 Those are commands for eg entering a new line etc. 这些是例如输入新行等的命令。

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

To make sure you don't use these regular expression operators put \\\\rho instead of \\rho . 为了确保您不使用这些正则表达式运算符,请使用\\\\rho而不是\\rho

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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