python - 如何将 Unicode 标题传递给 matplotlib?

标签 python matplotlib unicode python-2.x

无法在 matplotlib 中获取正确的标题: 'technologieën in °C' 给出:technologieën in ÃC

已经尝试过的可能解决方案:

  • u'technologieën in °C' 不起作用
  • 也没有:代码文件开头的 # -*- coding: utf-8 -*-

有什么解决办法吗?

最佳答案

你需要传入unicode文本:

u'technologieën in °C'

请确保您使用顶部的 # -*- coding: utf-8 -*- 注释,并确保您的文本编辑器实际使用该编解码器。如果您的编辑器将文件保存为 Latin-1 编码文本,请在 header 等中使用该编解码器。注释向 Python 传达如何解释您的源文件,尤其是在解析字符串文字时。

或者,对 Unicode 文字中的任何非 ASCII 使用转义码:

u'technologie\u00ebn in \u00b0C'

并首先避免使用什么编解码器的问题。

我建议你阅读:

在你继续之前。

大多数字体都支持 °,但如果您看到显示的是一个方框,则说明您遇到了字体问题,需要切换到支持您尝试显示的字符的字体。例如,如果 Ariel 支持您所需的字符,则使用:

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

在绘图之前。

关于python - 如何将 Unicode 标题传递给 matplotlib?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17525882/

相关文章:

python - Alphashape 和 PolygonPatch : basic example doesn't work. 为什么?

python-3.x - 按下 "Ctrl+C"时 python 回显服务器的 Unicode 错误

python - python configObj可以处理没有 '='的行吗

python - PyAutoGui 类型错误 : cannot unpack non-iterable NoneType object

python - 从向量场计算曲面

java - 注释中 unicode 转义序列的行为

python - Selenium webdriver 和 unicode

python - 无法进入lru_cache'_lru_cache_wrapper

python - 基于索引对多索引数据进行操作

Python 在 basemap 上绘制 shapefile