【问题标题】:How to include a string chain in a LaTeXed subscript in Python matplotlib [duplicate]如何在 Python matplotlib 的 LaTeXed 下标中包含字符串链 [重复]
【发布时间】:2016-04-01 13:11:03
【问题描述】:

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

【问题讨论】:

  • "{{({0},{1})}}".format

标签: python matplotlib format latex subscript


【解决方案1】:

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

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

Format String Syntax 供参考。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 2013-09-12
    • 2014-11-28
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    • 2015-04-15
    相关资源
    最近更新 更多