Matplotlib arrows between 3 points

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

Suppose we have 3 arbitrary points (A1, A2, A3) in 2d and 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), which receives the coordinates of the tail and arrow head and displays it?

+4
source share
1 answer

If you do not have additional special requirements for your desired method, you can use the pyplot arrow function , for example:

 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 a few more keyword arguments; even more style options can be found in the FancyArrow API (this is what the arrow actually creates under the hood).

Note that arrows can be turned off, as apparently pyplot will not necessarily adjust the x / y plot borders to show them. You may need to do this yourself through plt.xlim and plt.ylim .

+5
source

Source: https://habr.com/ru/post/1442726/


All Articles