我想在 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()
结果:
我从我系统上安装的字体中挑选了字体:
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/