Numpy's arctan2(y, x) will compute the counterclockwise angle (a value in radians between -ฯ€ and ฯ€) between the origin and the point (x, y).

You could do this for your points A and B, then subtract the second angle from the first to get the signed clockwise angular difference. This difference will be between -2ฯ€ and 2ฯ€, so in order to get a positive angle between 0 and 2ฯ€ you could then take the modulo against 2ฯ€. Finally you can convert radians to degrees using np.rad2deg.

import numpy as np

def angle_between(p1, p2):
    ang1 = np.arctan2(*p1[::-1])
    ang2 = np.arctan2(*p2[::-1])
    return np.rad2deg((ang1 - ang2) % (2 * np.pi))

For example:

A = (1, 0)
B = (1, -1)

print(angle_between(A, B))
# 45.

print(angle_between(B, A))
# 315.

If you don't want to use numpy, you could use math.atan2 in place of np.arctan2, and use math.degrees (or just multiply by 180 / math.pi) in order to convert from radians to degrees. One advantage of the numpy version is that you can also pass two (2, ...) arrays for p1 and p2 in order to compute the angles between multiple pairs of points in a vectorized way.

Answer from ali_m on Stack Overflow
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.angle.html
numpy.angle โ€” NumPy v2.4 Manual
>>> import numpy as np >>> np.angle([1.0, 1.0j, 1+1j]) # in radians array([ 0. , 1.57079633, 0.78539816]) # may vary >>> np.angle(1+1j, deg=True) # in degrees 45.0 >>> np.angle([0., -0., complex(0., -0.), complex(-0., -0.)]) # convention array([ 0. , 3.14159265, -0.
Top answer
1 of 8
41

Numpy's arctan2(y, x) will compute the counterclockwise angle (a value in radians between -ฯ€ and ฯ€) between the origin and the point (x, y).

You could do this for your points A and B, then subtract the second angle from the first to get the signed clockwise angular difference. This difference will be between -2ฯ€ and 2ฯ€, so in order to get a positive angle between 0 and 2ฯ€ you could then take the modulo against 2ฯ€. Finally you can convert radians to degrees using np.rad2deg.

import numpy as np

def angle_between(p1, p2):
    ang1 = np.arctan2(*p1[::-1])
    ang2 = np.arctan2(*p2[::-1])
    return np.rad2deg((ang1 - ang2) % (2 * np.pi))

For example:

A = (1, 0)
B = (1, -1)

print(angle_between(A, B))
# 45.

print(angle_between(B, A))
# 315.

If you don't want to use numpy, you could use math.atan2 in place of np.arctan2, and use math.degrees (or just multiply by 180 / math.pi) in order to convert from radians to degrees. One advantage of the numpy version is that you can also pass two (2, ...) arrays for p1 and p2 in order to compute the angles between multiple pairs of points in a vectorized way.

2 of 8
17

Use the inner product and the determinant of the two vectors. This is really what you should understand if you want to understand how this works. You'll need to know/read about vector math to understand.

See: https://en.wikipedia.org/wiki/Dot_product and https://en.wikipedia.org/wiki/Determinant

from math import acos
from math import sqrt
from math import pi

def length(v):
    return sqrt(v[0]**2+v[1]**2)
def dot_product(v,w):
   return v[0]*w[0]+v[1]*w[1]
def determinant(v,w):
   return v[0]*w[1]-v[1]*w[0]
def inner_angle(v,w):
   cosx=dot_product(v,w)/(length(v)*length(w))
   rad=acos(cosx) # in radians
   return rad*180/pi # returns degrees
def angle_clockwise(A, B):
    inner=inner_angle(A,B)
    det = determinant(A,B)
    if det<0: #this is a property of the det. If the det < 0 then B is clockwise of A
        return inner
    else: # if the det > 0 then A is immediately clockwise of B
        return 360-inner

In the determinant computation, you're concatenating the two vectors to form a 2 x 2 matrix, for which you're computing the determinant.

Discussions

Difficulty calculating angle between two points
ok so currently: def angle_between(a, b): return degrees(atan2(a[1] - b[1], b[0] - a[0])) I think it's because of how the atan2 function works. It will return a value [-pi, pi] rad. The reference starts in QI. So if your x2 is smaller than x1 what's likely happening is that your angle is flipping into Q3 or Q4 which is being reported as a negative angle by atan2 (since it's clockwise from the positive x-axis in the range [0, pi]). To fix this, try normalizing the angle if it's less than 0. So add 2pi (or 360 degrees). def angle_between(a, b): angle = degrees(atan2(a[1] - b[1], b[0] - a[0])) if angle < 0: angle += 360 return angle Trig was always my weakest subject so no promises! More on reddit.com
๐ŸŒ r/learnpython
11
2
November 8, 2023
Angles between two n-dimensional vectors in Python - Stack Overflow
For the few who may have (due to ... the angle between two lines in python, as in (x0, y0), (x1, y1) geometrical lines, there is the below minimal solution (uses the shapely module, but can be easily modified not to): from shapely.geometry import LineString import numpy as np ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Determining the angle between three points.
I'm trying to calculate the angle between two vectors. Two of the points are constant for each trial, (0,0) and (0,300), and the third point is given by the coordinates of participants' touch responses, which are saved as two separate variables, xpos and ypos. I'm trying to adapt this Python snippet to create a function that calculates the angle. import numpy ... More on forum.cogsci.nl
๐ŸŒ forum.cogsci.nl
May 9, 2017
math - python code to calculate angle between three point using their 3D coordinates - Stack Overflow
thanks Eric for your great help. kindly explain what is going on in line 10 (cosine_angle = -----), and how this code is more idiomatic numpy than adomas.m. I am very new in python. More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Medium
medium.com โ€บ @heyamit10 โ€บ calculating-the-angle-between-two-vectors-using-numpy-17e64256601c
Calculating the Angle Between Two Vectors Using NumPy | by Hey Amit | Medium
March 6, 2025 - import numpy as np # Define vectors A = np.array([0, 0]) # Zero vector B = np.array([3, 4]) # Calculate dot product and magnitudes dot_product = np.dot(A, B) magnitude_A = np.linalg.norm(A) magnitude_B = np.linalg.norm(B) # Check for zero vectors if magnitude_A == 0 or magnitude_B == 0: print("Cannot calculate angle with a zero vector.") else: angle_radians = np.arccos(dot_product / (magnitude_A * magnitude_B)) angle_degrees = np.degrees(angle_radians) print(f"Angle between A and B: {angle_degrees} degrees") ... Even if the calculation accidentally goes beyond those limits due to floating-poin
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.1 โ€บ reference โ€บ generated โ€บ numpy.angle.html
numpy.angle โ€” NumPy v2.1 Manual
>>> import numpy as np >>> np.angle([1.0, 1.0j, 1+1j]) # in radians array([ 0. , 1.57079633, 0.78539816]) # may vary >>> np.angle(1+1j, deg=True) # in degrees 45.0 >>> np.angle([0., -0., complex(0., -0.), complex(-0., -0.)]) # convention array([ 0. , 3.14159265, -0.
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ how to calculate the angle clockwise between two points
How to Calculate the Angle Clockwise Between Two Points - Be on the Right Side of Change
January 19, 2021 - The first thing we will do is import numpy as np, then define the angle using point 1 (p1) and point 2 (p2) as arguments. We will again use arctan2 multiplied by p1 to find angle 1 and arctan2 multiplied by p2 to find the second angle. We will return the degrees using the np.red2deg function ...
๐ŸŒ
Imperial College London
python.pages.doc.ic.ac.uk โ€บ 2021 โ€บ lessons โ€บ numpy โ€บ 08-summary โ€บ 05-angle.html
Introduction to NumPy and Matplotlib > Angle between two vectors | Python Programming (70053 Autumn Term 2021/2022) | Department of Computing | Imperial College London
Compute the dot product of these two vectors (giving you \cos(x)) Compute the \arccos of \cos(x) to get the angle in radians ... import numpy as np x = np.array([3, 4, 1, 5]) y = np.array([4, 1, 1, -1]) angle_deg = ????? assert np.isclose(angle_deg, 67.32549258300477) ... import numpy as np x = np.array([3, 4, 1, 5]) y = np.array([4, 1, 1, -1]) unit_x = x / np.linalg.norm(x) unit_y = y / np.linalg.norm(y) angle_rad = np.arccos(np.dot(unit_x, unit_y)) angle_deg = np.degrees(angle_rad)
Find elsewhere
๐ŸŒ
Forum
forum.cogsci.nl โ€บ discussion โ€บ 3076 โ€บ determining-the-angle-between-three-points
Determining the angle between three points. โ€” Forum
May 9, 2017 - I'm trying to calculate the angle between two vectors. Two of the points are constant for each trial, (0,0) and (0,300), and the third point is given by the coordinates of participants' touch responses, which are saved as two separate variables, xpos and ypos. I'm trying to adapt this Python snippet to create a function that calculates the angle. import numpy as np def get_angle(p0 = np.array([0.0,0.0]), p1 = np.array([0.0,300.0]), p2 = np.array([var.xpos, var.ypos]): v0 = np.array(p0) - np.array(p1) v1 = np.array(p2) - np.array(p1) angle = np.math.atan2(np.linalg.det([v0,v1]),np.dot(v0,v1)) return np.degrees(angle)
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-compute-the-angle-between-vectors-using-python
How to Compute the Angle Between Vectors Using Python - GeeksforGeeks
July 10, 2024 - Angle between vectors (in radians): ... the vector operations including the computing the angle between the vectors using the arccos function....
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ angles between two n-dimensional vectors in python
Angles between two n-dimensional vectors in Python - AskPython
February 27, 2023 - #importing required math module import math #calculating the magnitude of the vectors def mag(u, N): # Stores the final magnitude magnitude = 0 # Traverse the array for i in range(N): magnitude += u[i] * u[i] # Return the square root of magnitude return math.sqrt(magnitude) # Function to find the dot product of the vectors def dotProd(u, v, N): # Stores the dot product prod = 0 # Traverse the array for i in range(N): prod = prod + u[i] * v[i] # Return the product return prod def angleVector(u, v, N): # Stores the dot product of vectors dotProductOfVectors = dotProd(u, v, N) # magnitude of vect
๐ŸŒ
Medium
manivannan-ai.medium.com โ€บ find-the-angle-between-three-points-from-2d-using-python-348c513e2cd
Find the Angle between three points from 2D using python | by Manivannan Murugavel | Medium
March 25, 2019 - Now that formula, I will use for finding the angle between three points. We have use multiple dimentional data like 1D, 2D, 3D and higher dimensions not only 2D. But i explained with 2D data points. The dot product may be defined algebraically or geometrically. The geometric definition is based on the notions of angle and distance (magnitude of vectors). The equivalence of these two definitions relies on having a Cartesian coordinate system for Euclidean space.
๐ŸŒ
Python Pool
pythonpool.com โ€บ home โ€บ blog โ€บ numpy angle explained with examples
Numpy Angle Explained With Examples - Python Pool
December 30, 2023 - As per the definition, it only helps us in calculating the angle between the complex arguments. This means we cannot use this function to calculate the angle value between 2 points or vectors.
๐ŸŒ
Statology
statology.org โ€บ home โ€บ how to compute the angle between vectors using python
How to Compute the Angle Between Vectors Using Python
July 20, 2024 - The angle between two vectors represents the spatial relationship or orientation between them. A smaller angle means the vectors are more closely aligned, while a larger angle indicates they are pointing in more different directions.
๐ŸŒ
Medium
medium.com โ€บ @lennartluttkus_59686 โ€บ calculate-the-angle-between-two-vectors-in-python-and-numpy-2c25580bffc0
Calculate the angle between two vectors in python and NumPy | by Lennart Luttkus | Medium
February 15, 2022 - The problem was that due to the numerical inaccuracies, the argument for the arccos was outside the defined value range [-1, 1]. Therefore, I have added the numpy.clip function. Furthermore, I had to catch zero length vectors to avoid a division by zero. Usually, this angle would be undefined, but I set this angle to 0.0 ยท My solution takes into account the length of a vector and ยท def calculate_angle(self, vector_1, vector_2): """calculate the angle between two vectors in radians""" vector_1_length = np.linalg.norm(vector_1) vector_2_length = np.linalg.norm(vector_2) if (vector_1_length ==
Top answer
1 of 1
1

Seems to me that it works fine. Due to the fact that I dont see the character of you numpy/pandas coordinates array I cant give you exact solution

3 versions:

arctan

>>> direction = np.rad2deg(np.arctan((2-1)/(2-1)))
>>> direction
45.0

math

>>> direction = math.degrees(math.atan((2 - 1) / (2 - 1)))
>>> direction
45.0

arctan2

>>> direction = np.rad2deg(np.arctan2((2-1),(2-1)))
>>> direction
45.0

With fake data (as I dont see your data), using your function,

reference point:

>>> Xr = 10
>>> Yr = 10

Check direction towards reference point with these coordinates:

>>> Xs = np.array([1,2,3,4,5,6])
>>> Ys = np.array([1,2,3,4,5,6])

I am expecting all of them to have direction of 45 degrees

>>> direction = np.rad2deg(np.arctan2(Yr-Ys,Xr-Xs))
>>> direction
array([45., 45., 45., 45., 45., 45.])

EDIT

Based on your provided data:

It seems you were correct with the notion that there is different result.

More in here: numpy arctan2 bug or usage issues?

using np.arctan should be OK

numpy module

>>> direction = np.rad2deg(np.arctan(data.y/data.x))
>>> direction
0     45.819271
1     45.805556
2     45.795956
3     45.790496
4     45.785684
5     45.782246
6     45.771168
7     45.759343
8     45.744059
9     45.724642
10    45.712089
dtype: float64

math MODULE

>>> for i in range(10):
...     math.degrees(math.atan((data.y[i]) / (data.x[i])))
... 
45.81927143732942
45.805555571378584
45.79595585089821
45.79049607412479
45.78568399098587
45.782246280023934
45.771167675155795
45.75934340359498
45.744059202888714
45.72464168864543
>>> 

NOTE

Be careful about sign +-. Y2-Y1/X2-X1 vs Y1-Y2/X1-X2 can give different result, but in really both correct. In our case, these results are 45 deg or -135 deg. They both are correct, just one of them is clockwise