使用时没有可见文本()matplotlib.pyp

2024-06-16 13:20:59 发布

您现在位置:Python中文网/ 问答频道 /正文

matplotlib==1.5.2 pylab==0.1.3

我试图从课程"CS224d Deep Learning for NLP"Lecture 2中复制一张图。在

它应该如下所示:

enter image description here

我使用以下代码:

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()

然而,这些单词并没有出现在图表上。在

如果我删除指令

^{pr2}$
  • 我可以看到一些文本打印在角落,但轴的范围是错误的。在

如果我使用这个指令,那么即使我再次调用text(),我也看不到任何文本。在

我见过使用子批次并设置xy轴的精确范围的解决方案,但这似乎是不必要的复杂。在

我还能试试什么?在


Tags: text文本importfornlpmatplotlibasnp
1条回答
网友
1楼 · 发布于 2024-06-16 13:20:59

根据下面的答案,当您设置轴限制以显示文本时,它会显示单词。在

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()

相关问题 更多 >