python matplotlib标签/标题错误字符 [英] python matplotlib label/title wrong character
问题描述
这是此问题的部分交叉发布.
这是我的代码的最小示例:
This is a partial cross-post to this question.
Here is a minimal example of my code:
import matplotlib.pyplot as plt
x = [0.0, 0.25, 0.5, 0.75, 1.0]
y = [7.0, 3.0, 5.0, 1.0, 0.0]
II = 2
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y)
# un-comment title as needed:
#plot_title = r"A$_" + str(II) + r"$"
#plot_title = "A$_" + str(II) + "$"
plot_title = (r"A$_%s$" % (str(II)))
print plot_title
plt.title(plot_title)
plt.show()
同一plot-title-string有三种不同的版本.上面的print plot_title
给出了每种情况下正确的原始字符串:
There are three different versions of the same plot-title-string. The print plot_title
above gives the correct raw string for each case:
A$_2$
但是,这些方法都没有在图中显示正确的字符串(与我使用的后端无关).图中的输出揭示了输入整数值和输出之间的这种关系:
However, none of those approaches shows the correct string in the figure (independent of the back-end I use). The output in the figure reveals this relation between the input integer value and the output:
$0$ -> E
$1$ -> £
$2$ -> N
$3$ -> ®
$4$ -> X
$5$ -> ¸(cedille)
$6$ -> b
$7$ -> ¿
$8$ -> j
$9$ -> 3
我正在使用python 2.6.6和matplotlib 0.99.1.1-我无法控制这些版本,因此必须使用它们.
如何更改输入以获得所需的输出?
I am using python 2.6.6 and matplotlib 0.99.1.1 -- I have no control over these versions and will have to do with them.
How should I change my input to get the desired output?
修改
受到此问题/答案的启发,我尝试了系统知道的所有不同字体:
Inspired by this question/answer I tried all the different fonts my system knows:
import matplotlib.font_manager as font_manager
for i in range(0,len(sorted(font_manager.findSystemFonts()))):
plt.rcParams['font.family'] = os.path.basename(sorted(font_manager.findSystemFonts())[i])[:-4]
并为每种字体和上面的示例生成了一个图.虽然我为文本使用了不同的字体,但我关心的下标$_2$
并没有变化,并始终显示为 N
.
And generated a plot for each font and the above example. While I got different fonts for the text, the one subscript I care about, $_2$
, did not change and was always shown as N
.
编辑2
我已经升级到matplotlib 1.3.0,问题不再存在.这让我觉得它与0.99.1.1版本有关.
对我来说,这仍然不能令人满意,因为我想知道为什么为什么以这种方式工作.
EDIT 2
I have upgraded to matplotlib 1.3.0 and the issue is gone. This leaves me thinking it had to do with version 0.99.1.1
To me, this is still not satisfactory, because I would have liked to know why it was behaving in this way.
编辑3
我遇到了这个问题: Python图中的上标
答案表明,此问题可能是由于使用A$_2$
而不是$A_2$
引起的.
不幸的是,在这种情况下,这并没有改变输出.
EDIT 3
I came across this question: Superscript in Python plots
The answer suggests that the issue may arise from using A$_2$
instead of $A_2$
.
Unfortunately, this did not change the output in this case.
推荐答案
我正在使用matplotlib版本1.3.1和python版本2.7.8.
我有一个类似的问题.我在写
I am using matplotlib version 1.3.1 and python version 2.7.8.
I was having a similar problem. I was writing
ax1.set_yticklabels(['$10^4$','$10^5$','$10^6']$'
1将呈现为phi符号. 0将呈现为E.4将呈现为Xi ...,依此类推.
The 1 would render as a phi symbol. The 0 would render as E. The 4 would render as a Xi... so on.
我可以使用'$\mathregular{10}^\mathregular{4}$'
来修复"它,虽然有点麻烦,但确实可以完成.
I was able to 'fix' it by using '$\mathregular{10}^\mathregular{4}$'
which is kind of cumbersome but it does the job.
这篇关于python matplotlib标签/标题错误字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!