[英]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.