6

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 does: # -*- coding: utf-8 -*- at the beginning of the code-file.

Any solutions?

smci
  • 32,567
  • 20
  • 113
  • 146
Jomme
  • 1,256
  • 4
  • 14
  • 26
  • Works fine for me : http://ideone.com/nhrP0m – Ashwini Chaudhary Jul 08 '13 at 11:59
  • @AshwiniChaudhary: That's because you got lucky and pasted in the right encoding for the IDEOne.com output encoding. Or your terminal encoding. But a terminal or the IDEOne.com page encoding is not the same thing as matplotlib-generated output. – Martijn Pieters Jul 08 '13 at 12:01

2 Answers2

13

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • The only solution that works is this: u'technologie\u00ebn in \u00b0C'. # -*- coding: utf-8 -*- in combination with u'technologieën in °C' doesn't work. – Jomme Jul 08 '13 at 12:06
  • Then the encoding your text editor uses does *not* match the codec you specified in the comment at the top. If your editor uses the current windows code page, change it to `cp1252` (Windows Latin 1 variant) instead of UTF8, for example. Given that your text is Dutch (hoi!) that'd be my first guess. – Martijn Pieters Jul 08 '13 at 12:08
  • # -- coding: cp1252 -- doens't work. I work with Spyder. – Jomme Jul 08 '13 at 12:27
  • I have no idea what encoding Spyder uses when saving a file, sorry. – Martijn Pieters Jul 08 '13 at 12:45
  • Thanks for the help. Python and unicode is a real pain. Don't get it. .NET doesn't have that kind of trouble.... – Jomme Jul 08 '13 at 13:21
  • Yes you do but the tools hide it better. :-) – Martijn Pieters Jul 08 '13 at 13:23
  • (*Spyder dev here*) We use `utf-8` to save files in all major platforms (i.e. Windows, Linux and Mac) but we found a bug with PyQt4, Python 3 and unicode on Windows that will be fixed in our next release. Maybe that's what you're seeing here.... – Carlos Cordoba Oct 22 '14 at 02:01
0

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

HVNSweeting
  • 2,859
  • 2
  • 35
  • 30