gpt4 book ai didi

python - 使用 matplotlib.pyplot 时没有可见的 text()

转载 作者:太空宇宙 更新时间:2023-11-04 03:06:23 27 4
gpt4 key购买 nike

matplotlib==1.5.2pylab==0.1.3

我正在尝试从类(class)中复制图表 "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()

但是,这些词没有出现在图表上。

如果我删除指令

plt.autoscale()
  • 我可以看到角落里绘制了一些文本,但轴范围是错误的。

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

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

我还能尝试什么?

最佳答案

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

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

关于python - 使用 matplotlib.pyplot 时没有可见的 text(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39327258/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com