Python matplotlib label / title wrong character

This is a partial cross-post of this question .
Here's 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 header line. print plot_title

above gives the correct source line for each case:

A$_2$  

      

However, none of these approaches show the correct line in the figure (regardless of the one used in the background). The output signal in the figure shows this relationship 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 be related to them.
How do I change my input to get the desired output?

Edit

Inspired by this question / answer I've 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 created a graph for each font and above example. Although I have different fonts for the text, one index that I care about has $_2$

n't changed and always showed as N

.

EDIT 2
I updated to matplotlib 1.3.0 and the problem went away. This leaves me to think that it is related to version 0.99.1.1
For me it is still unsatisfactory because I would like to know why it behaves this way.

EDIT 3
I came across this question: Superscript in Python plots The
answer suggests that the problem might come from using A$_2$

instead $A_2$

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

+3


source to share


2 answers


I am using matplotlib version 1.3.1 and python version 2.7.8.
I had a similar problem. I wrote

ax1.set_yticklabels(['$10^4$','$10^5$','$10^6']$'

      



1 will display as phi symbol. 0 will display as E. 4 will display as Xi ... etc.

I was able to "fix" it using '$\mathregular{10}^\mathregular{4}$'

which is quite cumbersome, but it does the job.

+3


source


I had a similar problem. This minimal example:

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

      

displayed:

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

On this line, matplotlib uses LaTeX instead of the internal mathtext , 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, and is slower. Thus, this solution may only be useful if you have installed those prerequisites anyway.

I opened the problem here .

After some expensive tests, it turned 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 python-matplotlib-data

, but the spec is not specified, which leads to this issue. Although I don't have enough technical knowledge to be able to tell which font changes are incompatible with programs that use it.

+3


source







All Articles