无法在 Python 中绘制曲率的准确切线

问题描述

我有一个曲率数据集,我需要找到曲线的切线,但不幸的是,这离曲线有点远。请指导我与问题相关的问题解决方案。谢谢! 我的代码如下:

fig,ax1 = plt.subplots()
chData_m = efficient.get('Car.Road.y')  

x_fit = chData_m.timestamps
y_fit = chData_m.samples

fittedParameters = np.polyfit(x_fit[:],y_fit[:],1)

f = plt.figure(figsize=(800/100.0,600/100.0),dpi=100)
axes = f.add_subplot(111)

    # first the raw data as a scatter plot
axes.plot(x_fit,y_fit,'D')

    # create data for the fitted equation plot
xModel = np.linspace(min(x_fit),max(x_fit))
yModel = np.polyval(fittedParameters,xModel)

    # Now the model as a line plot
axes.plot(xModel,yModel)

axes.set_xlabel('X Data') # X axis data label
axes.set_ylabel('Y Data') # Y axis data label

    # polynomial derivative from numpy
deriv = np.polyder(fittedParameters)

    # for plotting
minX = min(x_fit)
maxX = max(x_fit)

    # value of derivative (slope) at a specific X value,so
    # that a straight line tangent can be plotted at the point
    # you might place this code in a loop to animate
pointVal = 10.0 # example X value
y_value_at_point = np.polyval(fittedParameters,pointVal)
slope_at_point = np.polyval(deriv,pointVal)

ylow = (minX - pointVal) * slope_at_point + y_value_at_point
yhigh = (maxX - pointVal) * slope_at_point + y_value_at_point

    # Now the tangent as a line plot
axes.plot([minX,maxX],[ylow,yhigh])

plt.show()
plt.close('all') # clean up after using pyplot

输出为:

enter image description here

解决方法

我不确定您想如何使用 numpy polyfit/polyval 来确定切线公式。我在这里描述了一种不同的方法。这种方法的优点是它对函数的性质没有任何假设。缺点是它不适用于垂直切线。
为了安全起见,我已经考虑了这两种情况,即评估的 x 值是您系列中的一个数据点,而事实并非如此。可能会出现一些问题,因为我看到您在问题中提到了时间戳,而没有通过提供玩具数据集来指定其性质 - 因此,此版本可能会或可能不会与原始数据的日期时间对象或时间戳一起使用:

import matplotlib.pyplot as plt
import numpy as np

#generate fake data with unique random x-values between 0 and 70
def func(x,a=0,b=100,c=1,n=3.5):
    return a + (b/(1+(c/x)**n))
np.random.seed(123)
x = np.random.choice(range(700000),100)/10000
x.sort()
y = func(x,1,2,15,2.4)


#data point to evaluate
xVal = 29

#plot original data
fig,ax = plt.subplots()
ax.plot(x,y,c="blue",label="data")

#calculate gradient
slope = np.gradient(y,x)

#determine slope and intercept at xVal
ind1 = (np.abs(x - xVal)).argmin()
#case 1 the value is a data point
if xVal == x[ind1]:
    yVal,slopeVal = y[ind1],slope[ind1]    
#case 2 the value lies between to data points
#in which case we approximate linearly from the two nearest data points
else:
    if xVal < x[ind1]:
        ind1,ind2 = ind1-1,ind1
    else:
        ind1,ind2 = ind1,ind1+1         
    yVal = y[ind1] + (y[ind2]-y[ind1]) * (xVal-x[ind1]) / (x[ind2]-x[ind1])
    slopeVal = slope[ind1] + (slope[ind2]-slope[ind1]) * (xVal-x[ind1]) / (x[ind2]-x[ind1])
intercVal = yVal - slopeVal * xVal    

ax.plot([x.min(),x.max()],[slopeVal*x.min()+intercVal,slopeVal*x.max()+intercVal],color="green",label=f"tangent\nat point [{xVal:.1f},{yVal:.1f}]\nwith slope {slopeVal:.2f}\nand intercept {intercVal:.2f}" )
ax.set_ylim(0.8 * y.min(),1.2 * y.max())
ax.legend()

plt.show()

enter image description here