Kumar
Kumar

Reputation: 69

Drawing parallel lines using Python?

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()

Upvotes: 2

Views: 5856

Answers (1)

Axel
Axel

Reputation: 1475

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

Upvotes: 2

Related Questions