2015-02-11 88 views
0

我有以下代码,当有标签有unicode字符串,注释失败抛出错误,我该如何解决?在Matplotlib注释中的Unicode字符串

from matplotlib import pyplot as plt 
import numpy as Math 


X = Math.genfromtxt(inputFile,autostrip=True,comments=None,dtype=Math.float64,usecols=(range(1,dim+1))) 
labels = Math.genfromtxt(inputFile,autostrip=True,comments=None,dtype='str',usecols=(0)) 
Y = some_function(X, 2, 50, 20.0);  
fig = plt.figure() 
ax = fig.add_subplot(111) 
plt.scatter(Y[:,0],Y[:,1]) 
for l,x,y in zip(labels,Y[:,0],Y[:,1]): 
    ax.annotate('(%s)' %l, xy=(x,y), textcoords='offset points') 

plt.grid() 
plt.show() 

Error : 
Traceback (most recent call last): 
ax.annotate('(%s)' %unicode(l), xy=(x,y), textcoords='offset points') 
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 4: ordinal not in range(128) 

回答

2

您需要的字符串为Unicode,而不是标准的ASCII(see here)解码:

from matplotlib import pyplot as plt 

l = '\xe2' 

plt.annotate('%s' % l, (0, 0)) 
# raises UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128) 

plt.annotate('%s' % l.decode('unicode-escape'), (0, 0)) 
# works 

你也可以解码输入文件为Unicode这样的:

# converter function that decodes a string as unicode 
conv = {0:(lambda s: s.decode('unicode-escape'))} 

labels = np.genfromtxt(inputFile, dtype='unicode', converters=conv, usecols=0) 

labels.dtype将会是unicode('<Ux')而不是字符串('|Sx'),因此ax.annotate('(%s)' %l, ...)将起作用。

+0

谢谢!第一个选项适用于我,但第二个选项不起作用 – Lanc 2015-02-11 16:54:20

+0

当你说它“不起作用”时,你能更具体吗?错误发生在'genfromtxt'还是'annotate'?如果你能显示你的'inputFile'在你的问题中看起来像什么会很有帮助。 – 2015-02-11 16:59:27

+0

我想出了问题 - 有必要将转换器参数传递给'genfromtxt',它将输入字符串解码为unicode。我已经更新了我的答案以包含此内容。 – 2015-02-11 17:31:23