Matplotlib中的Unicode字符串注释

时间:2015-02-11 12:38:48

标签: python numpy unicode matplotlib

我有以下代码,当标签有一个unicode字符串时,annotate会失败抛出错误,我该如何解决?

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)

1 个答案:

答案 0 :(得分: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, ...)将有效。