matplotlib图像的控制焦点

问题描述 投票:1回答:2

我正在尝试使用PCA注释我在2d模型中对Word2Vec建模的单词。变量result包含以下值:

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)

并且列表words为:

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

我尝试在其坐标中绘制单词的部分代码:

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

[当我尝试绘制这些单词时,x和y轴分别从(0,1)和(0,1)显示。如果只显示(0,0.2)和(0,0.2)或任何其他方式仅显示图像中存在点的部分会更好。

Image

python matplotlib pca
2个回答
0
投票

您需要在图的轴上设置范围:

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


0
投票

您可以使用它来设置x和y限制

axes.set_xlim([0,0.2])
axes.set_ylim([0,0.2])

© www.soinside.com 2019 - 2024. All rights reserved.