首页 > 解决方案 > matplotlib中箭头的位置

问题描述

以下代码从中心向某些点绘制箭头。但是,如您所见,箭头的位置在该点之后。我想把它放在重点之前。我该如何解决。

import matplotlib.pyplot as plt
import numpy as np

a = np.array([
[-0.108,0.414],
[0.755,-0.152],
],)

x, y = a.T
plt.scatter(x, y)
plt.xlim(-1,1)
plt.ylim(-1,1)

ax = plt.axes()
ax.arrow(0, 0, a[0,0], a[0,1], head_width=0.05, head_length=0.1, fc='k', ec='k')
ax.arrow(0, 0, a[1,0], a[1,1], head_width=0.05, head_length=0.1, fc='k', ec='k')
plt.grid(True)
plt.show()

在此处输入图像描述

标签: pythonmatplotlib

解决方案


使用length_includes_head=True.

ax.arrow(0, 0, a[0,0], a[0,1], head_width=0.05, head_length=0.1, fc='k', 
         ec='k', length_includes_head=True)
ax.arrow(0, 0, a[1,0], a[1,1], head_width=0.05, head_length=0.1, fc='k', 
         ec='k', length_includes_head=True)

输出:

在此处输入图像描述


推荐阅读