Matplotlib Python 3 未绘制从非原点开始的箭头

Matplotlib Python 3 is not drawing arrows starting at non-origin

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

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

这将从原点开始绘制箭头。但是,当我尝试从其他点绘制 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')

它没有画箭头!请问是什么问题?

谢谢。

使用matplotlib.axes.Axes.arrow

  • 这画了一个从 (x, y) 到 (x+dx, y+dy) 的箭头
  • 设置(-0.8,-0.8),得​​到如下

  • 为了改变箭头的方向,你必须补偿非零原点
  • 用下面一行:
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')
  • 会给你这个: