python - 如何在 Python matplotlib 中的 LaTeXed 下标中包含字符串链

标签 python matplotlib format latex subscript

<分区>

我想在 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_{{}}$ 解决这个问题,但它似乎不起作用,并且我没有更多的想法。

最佳答案

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

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

Format String Syntax供引用。

关于python - 如何在 Python matplotlib 中的 LaTeXed 下标中包含字符串链,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34480756/

相关文章:

c++ - 正则表达式:如何找到模式的最大整数值?

python - Django - 使用站点框架进行单元测试

python - 在 Django 中,objects.all() 不返回任何对象

python - 从列表python的末尾删除连续出现的事件

python - 如何在 matplotlib 中使用 unicode 符号?

javascript - 如何在javascript中将浮点值转换为日期时间格式

python - 设计一款可在浏览器中运行的网络游戏——我应该从哪里开始?

python - 如何从 3D numpy meshgrid 中提取 2D 平面

python - sklearn LogisticRegression - 绘图显示系数太小

java - 如何格式化字符串数字以在 android Edit Field 中使用逗号