matplotlib二维矩阵散点图及每个点标记

这篇博客介绍了如何在NLP作业中利用matplotlib库绘制散点图,并对每个点进行单词标注。通过提供的M_reduced矩阵和word2ind字典,代码遍历坐标并用annotate()函数添加文本标记,确保每个单词对应的散点清晰可见。
摘要由CSDN通过智能技术生成

记录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()
运行结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值