2

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:

enter image description here

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()
  • I can see some text plotted in the corner, but the axis range is wrong.

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?

1
  • 1
    Seems like plt.autoscale() does not work with text... Commented Sep 5, 2016 at 9:49

1 Answer 1

3

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.