Skip to content Skip to sidebar Skip to footer

How To Pass Unicode Title To Matplotlib?

Can't get the titles right in matplotlib: 'technologieën in °C' gives: technologieÃn in ÃC Possible solutions already tried: u'technologieën in °C' doesn't work neither doe

Solution 1:

You need to pass in unicode text:

u'technologieën in °C'

Do make sure you use the # -*- coding: utf-8 -*- comment at the top, and make sure your text editor is actually using that codec. If your editor saves the file as Latin-1 encoded text, use that codec in the header, etc. The comment communicates to Python how to interpret your source file, especially when it comes to parsing string literals.

Alternatively, use escape codes for anything non-ASCII in your Unicode literals:

u'technologie\u00ebn in \u00b0C'

and avoid the issue of what codec to use in the first place.

I urge you to read:

before you continue.

Most fonts will support the °, but if you see a box displayed instead, then you have a font issue and need to switch to a font that supports the characters you are trying to display. For example, if Ariel supports your required characters, then use:

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

before plotting.

Solution 2:

In Python3, there is no need to worry about all that troublesome UTF-8 problems.

One note that you will need to set a Unicode font before plotting.

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

Post a Comment for "How To Pass Unicode Title To Matplotlib?"