使用格式在 matplotlib 轴标签中添加多个下标字符

Adding multiple subscripted characters in matplotlib axis labels using format

我正在尝试向图形的 y 标签添加一些下标,并使用 format 完成它,这样我就不必手动制作多个图形。由于 matplotlib 允许 TeX 格式化,这很容易,但是用于下标或上标的多个字符的括号与 format 重叠。我使用的代码是

fig, ax = plt.subplots()
ax.plot(a,real_int, label='real int')
ax.plot(a,EM_int_sim, label='EM int')
ax.set_xlabel('t')
format_list = [l,j]
ax.set_ylabel(r"$\phi_{}$".format(*[l,j]))
ax.set_title("Offspring intensities")
ax.legend()

通常在 TeX 格式中我会使用 \phi_{lj} 来书写字符,但 format 也使用大括号。上面的代码只在下标中给出 l (这是我所期望的),但我也尝试过 \phi_{{}} 不给出任何字符, \phi_{}{} 只给出下标 l 但不是 j\phi_{}_{},它们给出了我见过的 longes 错误消息。有什么想法吗?

好的,所以如果您切换字符串插入方法,它就可以工作。此外,我将要插入的整数列表转换为字符串以使其工作。但是您也可以通过执行不同的 %s/%d/f..etc

来多次插入大括号
import matplotlib.pyplot as plt

fig, ax = plt.subplots(dpi=300)
ax.set_xlabel('t')
format_list = [0,1]

string_list=",".join([str(val) for val in format_list])

ax.set_ylabel(r"$\phi_{%s}$" % string_list)
ax.set_title("Offspring intensities")
ax.legend()