用Python画平行线?

2024-04-24 19:36:56 发布

您现在位置:Python中文网/ 问答频道 /正文

对于下面的代码,我如何使平行线与指定的距离。 给定第一行点A(0,7)B(5,2)
第二线(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()

Tags: the代码import距离plotmatplotlibasnp
1条回答
网友
1楼 · 发布于 2024-04-24 19:36:56

要得到直线的斜率,需要计算(y2 - y1) / (x2 - x1)。你正在做(y2 - x2) / (y1 - x1)。所以你只需要计算正确的斜率

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

坡度约为-0.42857。这将为您提供以下情节:

Plot of the two lines

相关问题 更多 >