views:

210

answers:

2

Does anyone know a way to get matplotlib to render accented chars (é,ã,â,etc)?

For instance i'm trying to use accented chars on set_yticklabels() and matplot renders squares instead, and when i use unicode() it renders the wrong chars.

Is there a way to make this work?

Thanks in advance, Jim.

Update

Turns out you can use u"éã" but first you have to set the file encoding

# Using the magic encoding
# -*- coding: utf-8 -*-

after that matplotlib correctly renders

u"é"

I also learned that you can use :

import matplotlib.font_manager as fm
fp1=fm.FontProperties(fname="/path/to/somefont.ttf")
ax.title("é",fontproperties=fp1)

in case you need to render a char that matplotlib does not have.

+3  A: 

Sure. You can use TeX:

from matplotlib import rcParams
rcParams['text.usetex'] = True
ax = ... # Axes object
ax.set_yticklabels(['$\'{e}$', '$\tilde{a}$', '$\hat{a}$'])
Steve
Using tex works, but since the text to be rendered is in a database (unicode) i will have to "convert" it every time, or there is a easier solution here?
OldJim
Oh, okay. Yeah, then this solution might be annoying. Use the other solution.
Steve
+4  A: 

Prefix the strings with u to tell Python that they are Unicode strings:

ax.set_yticklabels([u'é', u'ã', u'â'])
ptomato
Thanks for your input ptomato but it renders the wrong characters when i use u"é" or unicode('é','latin-1'), do these work for you?
OldJim
Yes, those work for me. As you say in your update, it was your file encoding that was causing the problem.
ptomato
Stackoverflow should allow questions to have multiple correct answers...
OldJim