首页 > 解决方案 > 控制 matplotlib 图像的焦点

问题描述

我正在尝试使用 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) 或任何其他方式显示图像中存在点的部分,那会更好。

图片

标签: pythonmatplotlibpca

解决方案


您需要在绘图轴上设置一个范围:

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

在此处输入图像描述


推荐阅读