Reputation: 521
I am trying to draw arrows between three points in matplotlib.
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.
Some code to make it clear:
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();
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?
Upvotes: 6
Views: 12267
Reputation: 2615
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)
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).
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.
Upvotes: 6