记录NLP作业中利用matplotlib绘制散点图并进行标记
原文:https://blog.csdn.net/wizardforcel/article/details/54782628
M_reduced是要求绘制的散点坐标 words 是单词 word2ind 字典,记录单词与坐标的对应关系
所使用的函数是annotate()
annotate(" 标记的文本 ", xy, xytext)
第一个参数是预标记的文本 第二个参数是预标注的点坐标 第三个参数是预标记文本的坐标
import numpy as np
import matplotlib.pyplot as plt
M_reduced= np.array([[1, 1], [-1, -1], [1, -1], [-1, 1], [0, 0]])
word2ind = {'test1': 0, 'test2': 1, 'test3': 2, 'test4': 3, 'test5': 4}
words = ['test1', 'test2', 'test3', 'test4', 'test5']
fig = plt.figure()
fig_sub1 = fig.add_subplot(111)
for i in range(len(words)):
fig_sub1.scatter(M_reduced[word2ind[words[i]],0],M_reduced[word2ind[words[i]], 1],color='r',marker='x')
plt.annotate(words[i], xy = (M_reduced[word2ind[words[i]], 0], M_reduced[word2ind[words[i]], 1]), xytext = (M_reduced[word2ind[words[i]], 0]+0.001, M_reduced[word2ind[words[i]], 1]+0.001))
plt.show()
运行结果
|