【问题标题】:Adding multiple subscripted characters in matplotlib axis labels using format使用格式在 matplotlib 轴标签中添加多个下标字符
【发布时间】:2020-07-24 09:59:48
【问题描述】:

我正在尝试为图形的 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和@ 987654331@ 给出了我见过的 longes 错误消息。有什么想法吗?

【问题讨论】:

  • l 和 j 是什么?字符串变量还是更多?
  • 不,只是整数(在这种情况下为 0 和 1)
  • 而您只想让 ylabel 读取下标为 0,1 的 phi?
  • 是的,就是这样

标签: python matplotlib axis-labels


【解决方案1】:

好的,所以如果您切换字符串插入的方法,它就可以工作。我还将您要插入的整数列表转换为字符串以使其工作。但是您也可以通过执行不同的 %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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-14
    • 1970-01-01
    • 1970-01-01
    • 2013-01-23
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多