如何在Python matplotlib的LaTeXed下标中包含一个字符串链[复制]

问题描述 投票:3回答:1

我想在matplotlib的图例中写一个使用LaTeX的带有下​​标的变量。这类问题的一个可能代码可能是:

import numpy as np
import matplotlib.pyplot as plt

a, b = 1, 0
string = "({0},{1})".format(n,l)
x = np.linspace(0, 2*np.pi, 1e3)

fig = plt.figure(0)
ax = plt.subplot(111)
ax.plot(x, np.sin(x), 'b', label = r"Function for $E_{}$".format(string))
ax.legend()
plt.savefig("example_fig.png")

但是,此代码生成一个图,其中只使用字符串链“string”的第一个元素:Example of the problem

我尝试使用.format{string[:]}$E_{{}}$来解决这个问题,但它似乎不起作用,我没有更多的想法。

python matplotlib format latex subscript
1个回答
1
投票

你需要使用三个大括号,一个大括号与另一个大括号逃脱,所以两个{{打印文字{。第三个被格式化了。

>>> string = '(1,2)'
>>> 'E_{{{}}}'.format(string)
'E_{(1,2)}'

Format String Syntax供参考。

© www.soinside.com 2019 - 2024. All rights reserved.