在 3 点之间绘制箭头 [英] Draw arrows between 3 points

查看:88
本文介绍了在 3 点之间绘制箭头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 matplotlib 中的三个点之间绘制箭头.

I am trying to draw arrows between three points in matplotlib.

假设我们在 2d 中有 3 个任意点 (A1,A2,A3),我们想要绘制从 A1 到 A2 以及从 A2 到 A3 的箭头.

Let's assume we have 3 arbitrary points (A1,A2,A3) in 2d and we want to draw arrows from A1 to A2 and from A2 to A3.

一些代码使其清晰:

import numpy as np
import matplotlib.pyplot as plt
A1=np.array([10,23])
A2=np.array([20,30])
A3=np.array([45,78])
drawArrow(A1,A2);
drawArrow(A2,A3);
plt.show();

我们如何编写一个函数 drawArrow(tailCoord,headCoord) 来接收箭头的尾部和头部的坐标并绘制它?

How can we write a function drawArrow(tailCoord,headCoord) that receives the coordinates of the tail and the head of an arrow and plots it?

推荐答案

除非你对你想要的方法有进一步的特殊要求,你可以使用

Unless you have further special requirements for your desired method, you can use pyplot's arrow function, e.g.:

def drawArrow(A, B):
    plt.arrow(A[0], A[1], B[0] - A[0], B[1] - A[1],
              head_width=3, length_includes_head=True)

API 提到了更多关键字参数;在用于FancyArrow的API (即什么箭头实际上在引擎盖下创建).

The API mentions some more keyword arguments; yet more style options can be found in the API for FancyArrow (which is what arrow actually creates under the hood).

请注意,箭头可能偏离绘图,因为显然 pyplot 不一定会调整绘图的 x/y 限制来显示它们.您可能必须通过 plt.xlimplt.ylim.

Note that the arrows might be off-plot, as apparently pyplot will not necessarily adjust the plot's x/y limits to show them. You might have to do this yourself via plt.xlim and plt.ylim.

这篇关于在 3 点之间绘制箭头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆