我正在使用 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 环境中无法识别包含“_”以生成下标。此外,我仍然需要 python/matplotlib 来识别这个下标命令应该影响以下变量。
有什么想法吗?
编辑
我做到了这一点:
axis_ylabel = plt.ylabel(u"\u03b1" + r"$_" + str(i) + r"$" + u" [\u00b0]", rotation='horizontal', position=(0.0,0.9))
——这会产生一个下标字符。但是,它不是整数值的转换,而是不同的符号。
编辑2
我正在使用 python 2.6.6 和 matplotlib 0.99.1.1。在 <> 处插入任何类型的字符串在 r"$<>$"不会导致显示该字符串,而是显示一个完全不同的字符。我已将此问题发布为 new question .
请您参考如下方法:
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:
ur'$\u23ce$'
