![](/img/trans.png)
[英]how to draw a line knowing the starting coordinate, line length x angle and y, angle
[英]How to find the angle of X and Y and then plot a line transecting each grid lines at a variable angle?
我正在尝试找出如何找到图像中标记的角度。 然后我希望能够 plot 一条与 y 轴或 x 轴以可变角度相交的线,比如 20 度?
我认为首先找到交叉点的角度可以帮助我自己弄清楚如何做变量位。
import matplotlib.pyplot as plt
f, ax = plt.subplots(figsize=(10, 10))
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('center')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
x = np.linspace(-5,5, 100)
plt.plot(x, -1.5*x+20, '-r', label='y=2x+1')
plt.plot(x, 2.5*x+4, '-r', label='y=2x+1')
plt.show()
编辑
这是重新排列正确答案中给出的公式的尝试。
以及我试图对其进行编纂的尝试,但它失败了。
my_angle = 45
gradient = np.arctan**(np.pi * my_angle/180 + np.pi)
由于您已经知道两条线的斜率,因此您可以简单地计算斜率的倒数 tan 来获得角度。 然后,您可以乘以180 / np.pi
以获得以度为单位的值。
import numpy as np
angle1 = np.arctan(2.5) * 180 / np.pi # angle in degrees
# 68.19859051364818
angle2 = 180 - abs(np.arctan(-1.5) * 180 / np.pi) # angle in degrees
# 123.69006752597979
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.