matplotlib==1.5.2 pylab==0.1.3
我正在尝试从类(class)中复制图表 "CS224d Deep Learning for NLP" , Lecture 2 .
它应该看起来像下面这样:
我正在使用以下代码:
import numpy as np
import matplotlib.pyplot as plt
la = np.linalg
words = ['I', 'like', 'enjoy', 'deep', 'learning', 'NLP', 'flying', '.']
X = np.array([[0,2,1,0,0,0,0,0],
[2,0,0,1,0,1,0,0],
[1,0,0,0,0,0,1,0],
[0,1,0,0,1,0,0,0],
[0,0,0,1,0,0,0,1],
[0,1,0,0,0,0,0,1],
[0,0,1,0,0,0,0,1],
[0,0,0,0,1,1,1,0]])
U, s, Vh = la.svd(X, full_matrices=False)
for i in xrange(len(words)):
plt.text(U[i,0], U[i,1], words[i])
plt.autoscale()
plt.show()
但是,这些词没有出现在图表上。
如果我删除指令
plt.autoscale()
- 我可以看到角落里绘制了一些文本,但轴范围是错误的。
如果我使用这条指令,那么我根本看不到任何文本,即使我再次调用 text() 也是如此。
我见过使用子图并为 x 和 y 轴设置精确范围的解决方案,但这似乎不必要地复杂。
我还能尝试什么?
请您参考如下方法:
当您设置轴限制以按照下面的答案显示文本时,它会显示单词。
import numpy as np
import matplotlib.pyplot as plt
la = np.linalg
words = ['I', 'like', 'enjoy', 'deep', 'learning', 'NLP', 'flying', '.']
X = np.array([[0,2,1,0,0,0,0,0],
[2,0,0,1,0,1,0,0],
[1,0,0,0,0,0,1,0],
[0,1,0,0,1,0,0,0],
[0,0,0,1,0,0,0,1],
[0,1,0,0,0,0,0,1],
[0,0,1,0,0,0,0,1],
[0,0,0,0,1,1,1,0]])
U, s, Vh = la.svd(X, full_matrices=False)
fig, ax = plt.subplots()
for i in xrange(len(words)):
ax.text(U[i,0], U[i,1], words[i])
ax.set_xlim([-0.8, 0.2])
ax.set_ylim([-0.8, 0.8])
plt.show()
