python中plot不能显示标签_python matplotlib标签/标题错误字符

1586010002-jmsa.png

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()

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

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?

Edit

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]

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.

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.

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.

解决方案

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']$'

The 1 would render as a phi symbol. The 0 would render as E. The 4 would render as a Xi... so on.

I was able to 'fix' it by using '$\mathregular{10}^\mathregular{4}$' which is kind of cumbersome but it does the job.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值