I have a problem displaying non-ASCII characters in Matplotlib, these characters are rendered as small boxes instead of a proper font, it looks like (I filled these boxes with red paint to hightlight them):
How do I fix it?
A related question is Accented characters in Matplotlib.
This problem may actually have a couple of different causes:
The default font does not include these glyphs
You may change the default font using the following (before any plotting is done!)
In some versions of matplotlib you'll have to set the family:
(Note that because
sans-serif
contains a hyphen inside the**{}
syntax, it is actually necessary.)The first command changes the
sans-serif
font family to contain only one font (in my case it was Arial), the second sets the default font family tosans-serif
.Other options are included in the documentation.
You have improperly created/passed string objects into Matplotlib
Even if the font contains proper glyphs, if you forgot to use
u
to create Unicode constants, Matplotlib will have this behaviour:So you need to add
u
:Another cause is that you forgot to put a UTF-8 magic comment on top of the file (I read that this might be the source of the problem):
jb.
's answer is good enough. In case you want to avoid addingu
, use this