matplotlib == 1.5.2 pylab == 0.1.3
我正在尝试从课程"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 轴的确切范围的解决方案,但这似乎不必要地复杂。
我还能尝试什么?
答案 0 :(得分: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)
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()