基于循环计数的python matplotlib轴标签下标

2024-04-26 23:20:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用python和matplotlib生成图形输出。 我正在一个循环中创建多个绘图,并希望循环计数器作为y轴标签上的索引。如何使循环计数器(变量)显示为下标?在

以下是我所拥有的:

axis_ylabel = plt.ylabel(u"\u03b1 [\u00b0]", rotation='horizontal', position=(0.0,0.9))

导致:

α[°]

(我使用unicode而不是Tex,因为dvipng在这个系统上不可用。)

我想要这样的东西:

for i in range(1,3):  
  axis_ylabel = plt.ylabel(u"\u03b1" + str(i) + u" [\u00b0]", rotation='horizontal', position=(0.0,0.9))

毫不奇怪,这会带来:

α1[°]
α2[°]

我真正想要的是作为下标的数字。如何将转换为字符串与创建下标的命令结合起来?在unicode环境中无法识别包含''uu'以生成下标。另外,我仍然需要python/matplotlib来识别这个下标命令应该影响以下变量。
有什么想法吗?在

编辑

我已经做到了:

^{pr2}$

--这将产生下标字符。然而,不是整数值的转换,而是不同的符号。在

编辑2
我使用的是python2.6.6和matplotlib0.99.1.1。在r"$<>$"中的<>处插入任何类型的字符串都不会导致该字符串的显示,而是一个完全不同的字符。我已将此问题发布为new question。在


Tags: 字符串命令编辑matplotlibunicode计数器positionplt
1条回答
网友
1楼 · 发布于 2024-04-26 23:20:04

Matplotlib提供自己的数学表达式引擎,称为mathtext
documentation

Note that you do not need to have TeX installed, since matplotlib ships its own TeX expression parser, layout engine and fonts.

因此,不妨尝试使用以下方法:

for i in range(1,3):
    plt.ylabel(
           r"$\alpha_{:d} [\degree]$".format(i),
           rotation='horizontal',
           position=(0.0,0.9))

您也可以在mathtext中使用Unicode:

If a particular symbol does not have a name (as is true of many of the more obscure symbols in the STIX fonts), Unicode characters can also be used:

^{pr2}$

相关问题 更多 >