Unicode or what in matplotlib?

走远了吗. 提交于 2019-12-10 14:55:28

问题


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' doens't work and neither does: # -*- coding: utf-8 -*- at the beginning of the code-file. Any solutions?


回答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:

  • The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) by Joel Spolsky

  • The Python Unicode HOWTO

  • Pragmatic Unicode by Ned Batchelder

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.




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



来源:https://stackoverflow.com/questions/17525882/unicode-or-what-in-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!