Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw arrows between 3 points

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?

like image 921
MHardy Avatar asked Oct 29 '12 11:10

MHardy


1 Answers

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.

like image 69
Hans Avatar answered Oct 21 '22 21:10

Hans