2016-11-14 41 views
2

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

我可以使用Unicode文本作为matplotlib的图块。 例如,

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) 
+1

您可能会觉得这篇文章有用:[Pragmatic Unicode](http://nedbatchelder.com/text/unipain.html),它是由SO老手Ned Batchelder编写的。 –

回答

1

该尔德链接是很好的了解,但如果你目前正在用自己的字符串绘制图形,而不是传入代码的图形,试试这个。该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