简体   繁体   English

如何找到 X 和 Y 的角度,然后找到 plot 一条以可变角度横切每条网格线的线?

[英]How to find the angle of X and Y and then plot a line transecting each grid lines at a variable angle?

I am trying to work out how to find the angles labelled in the image.我正在尝试找出如何找到图像中标记的角度。 I would then like to be able to plot a line intersecting the y or x-axis with a variable angle, say 20 degrees?然后我希望能够 plot 一条与 y 轴或 x 轴以可变角度相交的线,比如 20 度?

I think finding the angle of intersections first is the bit that could then help me work out how to do the variable bit myself.我认为首先找到交叉点的角度可以帮助我自己弄清楚如何做变量位。

在此处输入图像描述

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

Edit编辑

Here is an attempt to rearange the formula given in the correct answer.这是重新排列正确答案中给出的公式的尝试。

formula rearranged公式重新排列

and my attempt to codify it, but it breaks.以及我试图对其进行编纂的尝试,但它失败了。

my_angle = 45
gradient = np.arctan**(np.pi * my_angle/180 + np.pi)

Since you already know the slopes of your two lines, you can simply compute the inverse tan of the slopes to get the angles.由于您已经知道两条线的斜率,因此您可以简单地计算斜率的倒数 tan 来获得角度。 You can then multiply with 180 / np.pi to get the values in degrees.然后,您可以乘以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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM