Schorsch Schorsch - 1 year ago 81
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)

# 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

There are three different versions of the same plot-title-string. The
print plot_title
above gives the correct raw string for each case:


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 -- 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[''] = 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,
, did not change and was always shown as


I have upgraded to matplotlib 1.3.0 and the issue is gone. This leaves me thinking it had to do with version

To me, this is still not satisfactory, because I would have liked to know why it was behaving in this way.


I came across this question: Superscript in Python plots

The answer suggests that the issue may arise from using
instead of

Unfortunately, this did not change the output in this case.

Answer Source

I am using matplotlib version 1.3.1 and python version 2.7.8.
I was having a similar problem. I was writing


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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download