Schorsch Schorsch - 3 months ago 9
Python Question

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


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.

Answer

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.

Comments