【问题标题】:string variable as latex in pyplot字符串变量作为pyplot中的乳胶
【发布时间】:2017-11-04 02:37:19
【问题描述】:

可以在 pyplot 图表中使用 Latex 格式,即标题、标签等。

#some dummy code
plt.plot(x,y,label = r"$a < \gamma$")

它是通过在实际的乳胶字符串之前添加一个 r 生成的。 现在,pyplot 接受变量作为字符串的输入,即

#some dummy code
foo = "some fancy label"
plt.plot(x,y, label = foo)

我想将这两个世界结合起来,但是我在 SO 上找到的唯一答案是 Julia。

我怎样才能做到这一点?

【问题讨论】:

    标签: python python-2.7 matplotlib latex


    【解决方案1】:

    示例如下:

    foo = r"some fancy label and $a < \gamma$"
    plt.plot(x,y, label = foo)
    

    添加r 的目的是使字符串成为raw string (literal)。这是必要的,因为\ 不是有效的字符串文字。但是,\ 可以被另一个\ 转义,因此以下内容也有效

    foo = "some fancy label and $a < \\gamma$"
    plt.plot(x,y, label = foo)
    

    请注意,字符串前面没有r

    还要注意美元符号$ 实际上并不是调用乳胶,而是调用MathText。为了获得真正的乳胶渲染,您需要安装一个工作乳胶并调用usetex=True 参数;详情here.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多