Matplotlib Python 3没有从非原点开始绘制箭头

2022-12-01 03:45:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码来绘制单词向量:

import numpy as np
import matplotlib.pyplot as plt
la = np.linalg

words = ['I', 'like', 'enjoy', 'deep', 'learning', 'NLP', 'flying', '.']
X = np.array([ [0,2,1,0,0,0,0,0],
               [2,0,0,1,0,1,0,0],
               [1,0,0,0,0,0,1,0],
               [0,1,0,0,1,0,0,0],
               [0,0,0,1,0,0,0,1],
               [0,1,0,0,0,0,0,1],
               [0,0,1,0,0,0,0,1],
               [0,0,0,0,1,1,1,0]])
U, s, Vh = la.svd(X, full_matrices = False)
ax = plt.axes()
for i in range(len(words)): 
    plt.text(U[i,0],U[i,1],words[i])
    ax.arrow(0,0,U[i,0],U[i,1],head_width=0.1, head_length=0.1, fc='lightblue', ec='black')
plt.xlim(-.8,.2)
plt.ylim(-.8,.8)
plt.grid()

plt.title(' Simple SVD word vectors in Python',fontsize=10)
plt.show()
plt.close()

enter image description here

这将从原点开始绘制箭头。但是,当我试图从其他点来绘制if时。你知道吗

ax.arrow(-0.8,-0.8,U[i,0],U[i,1],head_width=0.1, head_length=0.1, fc='lightblue', ec='black')

enter image description here

它不画箭头!请问有什么问题?你知道吗

谢谢你。你知道吗


Tags: inimportasnp绘制pltaxwidthlengthheadlalightbluefcwordsarrow
1条回答
网友
1楼 · 发布于 2022-12-01 03:45:41

使用^{}

  • 这将绘制一个从(x,y)到(x+dx,y+dy)的箭头
  • 设置(-0.8,-0.8),得到以下结果

enter image description here

  • 为了改变箭头的方向,必须补偿非零原点
  • 使用以下行:
ax.arrow(-0.8, -0.8, (U[i,0] + 0.8), (U[i,1] + 0.8),head_width=0.1, head_length=0.1, fc='lightblue', ec='black')
  • 我会给你这个:

enter image description here