Search code examples
pythonmatplotlibpca

Controlling focus of matplotlib image


I am trying to use PCA to annotate the words I modeled Word2Vec in 2d. The variable result contains the value below :

array([[ 0.01632784,  0.01212493],
       [ 0.00070532,  0.01451515],
       [-0.0055863 , -0.00661636],
       [-0.01106532, -0.0157193 ],
       [-0.01473162,  0.00611054],
       [-0.01046929,  0.01837107],
       [-0.01007252, -0.00692229],
       [ 0.00529983, -0.0078546 ],
       [ 0.00972514, -0.0030543 ],
       [ 0.01812323, -0.01013864],
       [-0.00453239, -0.00411107],
       [-0.00108769, -0.00255492],
       [ 0.0009    ,  0.00191122],
       [ 0.00646378,  0.00393857]], dtype=float32)

and the list words is :

'Text',
 'of',
 'the',
 'first',
 'document',
 'second',
 'made',
 'longer',
 'Number',
 'three',
 'This',
 'is',
 'number',
 'four']

Part of the code where I have tried to plot the words in their coordinates :

import matplotlib.pyplot as plt
for i,word in enumerate(words):
    plt.annotate(word, xy=(result[i,0], result[i,1]))
plt.show()

When I try to plot these words, the x and y axis are displayed from (0,1) and (0,1) respectively. It would be better if I could display only from (0,0.2) and (0,0.2) or any other way to only display the part of the image where points are present.

Image


Solution

  • What you need is to set a range on the plot's axis:

    for i,word in enumerate(words):
        plt.annotate(word, xy=(result[i,0], result[i,1]))
    plt.ylim(-0.04, 0.05)
    plt.xlim(-0.04, 0.05)
    plt.show()
    

    enter image description here