2

For the below code how I can make parallel lines with a specified distance. given first line points A(0,7) B(5,2)
Second line(3,2)

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

x=[0, 7]
y=[5, 2]
plt.plot(x,y)

o = np.subtract(2, 7)
q = np.subtract(5, 0)
slope = o/q

#(m,p) are the new coordinates to plot the parallel line
m = 3
p = 2

axes = plt.gca()
x_val = np.array(axes.get_xlim())
y_val = np.array(slope*(x_val - m) + p)
plt.plot(x_val,y_val, color="black", linestyle="--")
plt.show()
5
  • 1
    Out of curiosity, any particular reason you're doing o = np.subtract(2,7) instead of o = 2 - 7? Commented Apr 17, 2018 at 11:58
  • Yes, I want to modify it later with variables. Commented Apr 17, 2018 at 12:02
  • 3
    Ok, but ordinary subtraction can also involve variables. Commented Apr 17, 2018 at 12:05
  • ok i will edit that. Commented Apr 17, 2018 at 12:10
  • 1
    If the lines are parallel, we know that the slope is constant. What is m and p in terms of x and y? Are you familiar with the concept of intercept? Commented Apr 17, 2018 at 12:13

1 Answer 1

2

To get the slope of your line you need to calculate (y2 - y1) / (x2 - x1). You are doing (y2 - x2) / (y1 - x1). So you just need to calculate the correct slope by

o = np.subtract(2, 5)  # y[1] - y[0]
q = np.subtract(7, 0)  # x[1] - x[0]
slope = o/q

which will give a slope of approx. -0.42857. This will give you the following plot:

Plot of the two lines

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.