字符串变量作为pyplot中的乳胶 [英] string variable as latex in pyplot
问题描述
有可能在pyplot图中具有Latex格式,即标题,标签等.
It is possible to have Latex formatting in pyplot diagrams, i.e. for title, labels etc.
#some dummy code
plt.plot(x,y,label = r"$a < \gamma$")
它是通过在实际的乳胶字符串之前添加一个r来生成的.现在,pyplot接受变量作为字符串的输入,即
It is generated by adding an r before the actual latex string. Now, pyplot accepts variables as inputs for strings, i.e.
#some dummy code
foo = "some fancy label"
plt.plot(x,y, label = foo)
我想将这两个世界结合起来,但是我在 SO 上找到的唯一答案是 Julia.
I would like to combine the two worlds, however the only answer I found on SO is for Julia.
我该如何进行这项工作?
How can I make this work?
推荐答案
示例如下:
foo = r"some fancy label and $a < \gamma$"
plt.plot(x,y, label = foo)
The point of adding an r
is that it makes the string a raw string (literal). This is necessary because \
is no valid string literal. However, \
can be escaped by another \
, such that the following is valid as well
foo = "some fancy label and $a < \\gamma$"
plt.plot(x,y, label = foo)
请注意,字符串前面没有 r
.
Note that there is no r
in front of the string.
还请注意,美元符号 $
实际上不是在调用乳胶,而是所谓的 MathText .为了获得真实的乳胶渲染,您需要安装可运行的乳胶并调用 usetex = True
参数.详细信息此处.
Also note that the dollar signs $
are not actually invoking latex but what is called MathText. In order to get true latex rendering you would need to have a working latex installation and invoke the usetex=True
parameter; details here.
这篇关于字符串变量作为pyplot中的乳胶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!