Schorsch
Schorsch

Reputation: 7915

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.

Upvotes: 3

Views: 2433

Answers (2)

mxmlnkn
mxmlnkn

Reputation: 2141

I had a similar problem. This minimal example:

from matplotlib.pyplot import *
subplot( 111, title=r"$\mathcal{O}\left( N^3 \right)$" )
show()

was rendered to:

enter image description here

Adding matplotlib.rc('text', usetex=True) at the beginning solved the problem for me:

from matplotlib.pyplot import *
matplotlib.rc('text', usetex=True)
subplot( 111, title=r"$\mathcal{O}\left( N^3 \right)$" )
show()

enter image description here

This line makes matplotlib use LaTeX instead of the internal mathtext engine, which seems to be a subset of LaTeX, although superscripts should still work. Using LaTeX requires a working LaTeX installation, as well as dvipng and ghostscript, also it is slower. So this solution might only be worth trying if you have these prerequisites installed anyway.

I opened an issue here.

After some time-costly tests I found out, that downgrading from fonts-lyx 2.2.0-2 to 2.1.4-2 also solved the problem for me. fonts-lyx is a dependency of python-matplotlib-data, but no specific version is specified, resulting in this problem. Although I don't have enough techical knowledge to be able to say what changes to a font are incompatibel with programs using it.

Upvotes: 3

Alice
Alice

Reputation: 56

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.

Upvotes: 4

Related Questions