matplotlib==1.5.2 pylab==0.1.3
I am trying to reproduce a graph from the course "CS224d Deep Learning for NLP", Lecture 2.
It should look the following way:
I am using the following code:
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()
However, the words don't appear on the graph.
If I remove the instruction
plt.autoscale()
If I use this instruction, then I see no text at all, even if I call text() once again.
I have seen solutions with using subplots and setting the exact ranges for x and y axis, but this seems to be unnecessarily complex.
What else can I try?
It shows the words when you set axis limits to show the text as per this answer below.
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()
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments