H.A*_*han 2 python plot matplotlib
我基本上想从坐标(x,y)绘制具有给定角度的线(计算切线值)。
使用这样的简单代码行,pl.plot([x1, x2], [y1, y2], 'k-', lw=1)我可以在两点之间绘制一条线,但是为此,我需要计算(x2,y2)坐标。我的(x1,y1)坐标是固定的,并且角度已知。计算(x2,y2)会在某个点上引起问题,因此我只想从(x1,y1)绘制角度(最好是长度)的线。
我想到的最简单的解决方案是使用点斜率函数y - y1 = m(x - X1)。解释这些并进行一些搜索,我使用了这段代码:
x1 = 10
y1 = -50
angle = 30
sl = tan(radians(angle))
x = np.array(range(-10,10))
y = sl*(x-x1) + y1
pl.plot(x,y)
pl.show
Run Code Online (Sandbox Code Playgroud)
sl是坡度,x1和y1是坐标。我需要自我解释,因为这是一个很糟糕的问题。
所以,现在,关于我该如何解决的任何想法?
我不确定自己从解释中究竟想要什么,但是我认为这将完成您所要求的工作。
如果知道要使用的线的角度和长度,则应使用三角函数获取新点。
import numpy as np
import math
import matplotlib.pyplot as plt
def plot_point(point, angle, length):
'''
point - Tuple (x, y)
angle - Angle you want your end point at in degrees.
length - Length of the line you want to plot.
Will plot the line on a 10 x 10 plot.
'''
# unpack the first point
x, y = point
# find the end point
endy = length * math.sin(math.radians(angle))
endx = length * math.cos(math.radians(angle))
# plot the points
fig = plt.figure()
ax = plt.subplot(111)
ax.set_ylim([0, 10]) # set the bounds to be 10, 10
ax.set_xlim([0, 10])
ax.plot([x, endx], [y, endy])
fig.show()
Run Code Online (Sandbox Code Playgroud)