python - 在 seaborn 中使用 Unicode 文本

标签 python python-2.7 matplotlib seaborn

我想在 Seaborn 中使用 Unicode 文本。 ( python 2.7)

我可以将 Unicode 文本用作 matplotlib 的图 block 。 例如,

import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
fp = FontProperties(fname='/usr/share/fonts/truetype/takao-gothic/TakaoGothic.ttf')
text = u'bădărău'
plt.plot([1,2,3,2], label=text)
plt.legend(prop=fp) 

如何给seaborn设置这种字体属性? 实际上,我想在以下示例中使用 Unicode 文本作为注释:

import seaborn as sns
import numpy as np
sns.heatmap(np.array([[1,2,3]]), annot=np.array([['a', 'b', 'c']]), fmt='')
# want to use ă instead of a

如果我使用 ă,我会得到以下错误

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 0: ordinal not in range(128)

最佳答案

Batchelder 链接很值得了解,但如果您目前正在使用自己的字符串而不是传递到代码中的字符串来绘制图表,请试试这个。 matplotlib documentation解释如何在源代码中使用 unicode 文字:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import seaborn as sns
from matplotlib.pyplot import show
import numpy as np

sns.set(font="Meiryo")
sns.heatmap(np.array([[1,2,3]]), annot=np.array([['ë', 'bădărău', 'ê']]),fmt='')
show()

结果:

enter image description here

我从我系统上安装的字体中挑选了字体:

from matplotlib import font_manager

font_paths = font_manager.findSystemFonts()
font_objects = font_manager.createFontList(font_paths)
font_names = [f.name for f in font_objects]
print font_names

来自 another SO .

关于python - 在 seaborn 中使用 Unicode 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40580584/

相关文章:

Python pygame - 弹跳球(UnboundLocalError : local variable 'move_y' referenced before assignment)

python - int 的任意基数字列表

python - Python BDD 工具的 Bazel 测试规则 "behave"

python - 为什么 pip 不适合我?

windows - 用于日志记录的基于 Python 时间的旋转文件处理程序

python - 将 Tix 小部件添加到 Tkinter 容器

python - Matplotlib 中的像素化动画

python - Matplotlib - 匹配轴单位长度(分辨率)

python - 新手 :invalid literal in python

python - 我无法在 anaconda 环境中安装 Jupyter 和 Matplotlib