Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-ASCII characters in Matplotlib

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):

Here is the image showing the problem

How do I fix it?

A related question is Accented characters in Matplotlib.

like image 260
jb. Avatar asked Jun 09 '12 11:06

jb.


People also ask

How do you get non-ASCII characters in Python?

In order to use non-ASCII characters, Python requires explicit encoding and decoding of strings into Unicode. In IBM® SPSS® Modeler, Python scripts are assumed to be encoded in UTF-8, which is a standard Unicode encoding that supports non-ASCII characters.

What is a non-ASCII character?

Non-ASCII characters are those that are not encoded in ASCII, such as Unicode, EBCDIC, etc. ASCII is limited to 128 characters and was initially developed for the English language.

How do you remove non-ASCII characters?

Use . replace() method to replace the Non-ASCII characters with the empty string.


1 Answers

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!)

matplotlib.rc('font', family='Arial') 

In some versions of matplotlib you'll have to set the family:

matplotlib.rc('font', **{'sans-serif' : 'Arial',                          'family' : 'sans-serif'}) 

(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 to sans-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:

plt.xlabel("Średnia odległość między stacjami wsparcia a modelowaną [km]") 

So you need to add u:

plt.xlabel(u"Średnia odległość między stacjami wsparcia a modelowaną [km]") 

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):

# -*- coding: utf-8 -*- 
like image 147
jb. Avatar answered Sep 20 '22 08:09

jb.