9

I basically want to plot a line from a coordinate (x, y) with a given angle (calculating the tangent value).

With a simple line of code like this pl.plot([x1, x2], [y1, y2], 'k-', lw=1) I can plot a line between two points but for this I need to calculate (x2, y2) coordinate. My (x1, y1) coordinate is fixed and the angle is known. Calculating (x2, y2) causes a problem at some point so I just want to plot the line from (x1, y1) with an angle (and preferably with a length).

The simplest solution I came up with that was to use point-slope function which is y - y1 = m(x - X1). Interpreting thiss and searching a little I used this piece of code:

x1 = 10
y1 = -50
angle = 30

sl = tan(radians(angle))
x = np.array(range(-10,10))
y = sl*(x-x1) + y1

pl.plot(x,y)
pl.show

sl is here slope and x1 and y1 are the coordinates. I needed to explain myself since this found to be a poor question.

So now, any ideas on how I can do/solve that?

1
  • 1
    Yep, did it! I wouldn't post it otherwise. The simplest solution that came to my mind was to use the equation of a line with slope and one coordinate known. In that case I decided to do this: x1 = 10 y1 = -50 angle = 30 sl = tan(radians(angle)) x = np.array(range(-10,10)) y = sl*(x-x1) + y1 pl.plot(x,y) pl.show Here y is the equation of line and sl is the slope of that line. However that doesn't seem to work. Feb 10, 2015 at 16:18

4 Answers 4

12

Update version 2023

I'm not really sure what exactly you want from the explanation, but I think this will do something close to what you asked for.

You should use trigonometry to get the new point if you know the angle and length of a line you want to use.

import numpy as np
import math
import matplotlib.pyplot as plt


def plot_point(point, angle, length):
     '''
     point - Tuple (x, y)
     angle - Angle you want your end point at in degrees.
     length - Length of the line you want to plot.

     Will plot the line on a 10 x 10 plot.
     '''

     # unpack the first point
     x, y = point

     # find the end point
     endy = y + length * math.sin(math.radians(angle))
     endx = x + length * math.cos(math.radians(angle))

     # plot the points
     fig = plt.figure()
     ax = plt.subplot(111)
     ax.set_ylim([0, 10])   # set the bounds to be 10, 10
     ax.set_xlim([0, 10])
     ax.plot([x, endx], [y, endy])

     fig.show()
5
  • 3
    You forgot to add the starting coordinates to the end: endy = y + ...
    – hitzg
    Feb 10, 2015 at 21:40
  • That's what I am currently doing in my code. Given the slope, I calculate another coordinate and plot between two points as I have given in my question, same as you offered. But I do not want to include those calculations in my code. That is why I was wondering if there is a library I can use from which I can draw a line with one coordinate, angle and the length. Feb 17, 2015 at 23:06
  • 1
    @H.AzizKayıhan Why do you want / need a library when you can create a simple def by yourself? Mar 2, 2021 at 8:26
  • Please, change it to endx = x + length * math.cos(math.radians(angle)). I spent a long time debugging my code because of this simple omission.
    – TheBoyne
    Feb 20 at 2:51
  • There is now an axline feature in matplotlib for these kinds of plots, see the answer by famaral42.
    – Erik
    Aug 23 at 9:35
2

Inspired by this website, given a WGS84 coordinate, a bearing (sometimes referred to as forward azimuth) and a distance, you can compute a resulting destination point with the following logic:

import math 

distance = 100 # kilometres
radius = 6371 # earth's radius in kilometres

lon, lat = -7.83197, 37.040893
bearing = 40

δ = distance / radius
θ = math.radians(bearing)

φ1 = math.radians(lat)
λ1 = math.radians(lon)

sinφ2 = math.sin(φ1) * math.cos(δ) + math.cos(φ1) * math.sin(δ) * math.cos(θ)
φ2 = math.asin(sinφ2)
y = math.sin(θ) * math.sin(δ) * math.cos(φ1)
x = math.cos(δ) - math.sin(φ1) * sinφ2
λ2 = λ1 + math.atan2(y, x)

lat2 = math.degrees(φ2)
lon2 = math.degrees(λ2)

Which will yield

>>> lon2, lat2
(-7.831861171142511, 37.04091627610624)

destination point

1

Standard module for complex numbers, cmath, makes it easy.

   import cmath

   pt = cmath.rect(r, angle)  
   x = pt.real  
   y = pt.imag

Given the length of the line (or radius), r, and the angle in radians, we can get the terminal point x and y coordinates (x, y) for the line starting from origin (0, 0).

  • Not starting from origin: if line starts from any other point (x1, y1), simply add to get (x2, y2) as x2 = x1 + x and y2 = y1 + y

  • Degrees to Radians: if angle is available in degrees, use math.radians(deg) to get the same in radians. Of course, remember to import math before use.

cmath.rect(r, phi) is the function you will call. It returns a complex number! Simply take its real and imaginary parts as the x and y values you need.

1

What you want is kind new and is called axline.

import numpy as np
import matplotlib.pyplot as plt

x1 = 10
y1 = -50
angle = 30

sl = np.tan(np.radians(angle))

x = np.arange(-10,10)
y = sl*(x-x1) + y1

plt.plot(x,y, 'o', label='manual')

plt.axline((x1,y1), slope=sl, color='red', label='axline')

plt.legend()
plt.grid()
plt.show()
2
  • no module named numpy?
    – Aceplante
    Oct 2, 2021 at 2:29
  • 1
    ? You ran this code and got that message? ""no module named numpy""? if that's the case, you should install the module numpy and (probably) matplotlib. A simple "pip install numpy matplotlib" should work. But I would suggest you look Anaconda (Python distribution)
    – famaral42
    Oct 3, 2021 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.