使用np.polyfit进行多项式回归后,计算并绘制曲线上的切线。

3
使用np.polyfit拟合数据系列,并使用np.polyval评估其绘制的曲线,如下所示:enter image description here。如何在曲线上的某一点计算切线,并如何以系列中的x和y值沿曲线动画一系列切线?感谢James Phillips的解决方案,下面绘制了多项式曲线上的切线:Tangent line on a polynomial curve
x     y
0     21.05
1     21.21
2     20.76
3     20.34
4     20.27
5     20.78
6     20.60
7     20.55
8     19.95
9     19.23
10    19.64
11    19.92
12    19.91
13    19.56
14    19.39
15    19.31
16    19.35
17    18.97
18    18.69
19    19.00
20    19.15
21    19.08
22    18.97
23    19.26
24    19.52
25    19.56
26    19.28
27    19.47
28    19.85
29    19.77
1个回答

7

以下是使用numpy的polyder()函数示例代码,可以自动对多项式进行微分,这样您就不需要手动计算,非常方便在开发过程中更改多项式阶数。此代码可绘制给定“X”值处的数据、方程和切线,这应该足以让您入门了。虽然我不知道您选择的动画技术,但我个人会将图像序列保存为PNG文件,转换为GIF,然后在我的zunzun.com网站上使用gifsicle进行3D表面绘制旋转动画。

import numpy, matplotlib
import matplotlib.pyplot as plt

xData = numpy.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0])
yData = numpy.array([21.05, 21.21, 20.76, 20.34, 20.27, 20.78, 20.60, 20.55, 19.95, 19.23, 19.64, 19.92, 19.91, 19.56, 19.39, 19.31, 19.35, 18.97, 18.69, 19.00, 19.15, 19.08, 18.97, 19.26, 19.52, 19.56, 19.28, 19.47, 19.85, 19.77])

# polynomial curve fit the test data
fittedParameters = numpy.polyfit(xData, yData, 3)


##########################################################
# graphics output section
def ModelAndScatterPlot(graphWidth, graphHeight):
    f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
    axes = f.add_subplot(111)

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

    # create data for the fitted equation plot
    xModel = numpy.linspace(min(xData), max(xData))
    yModel = numpy.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 = numpy.polyder(fittedParameters)

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

    # 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 = 15.0 # example X value
    y_value_at_point = numpy.polyval(fittedParameters, pointVal)
    slope_at_point = numpy.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


graphWidth = 800
graphHeight = 600
ModelAndScatterPlot(graphWidth, graphHeight)

1
感谢James提供的示例代码和关于GIF的建议。我可以看到这个示例中的效果:https://i.stack.imgur.com/2IWnf.gif :) - artDeco
1
我这里有一些matplotlib的GIF动画:http://zunzun.com/CommonProblems/,完整的源代码在这里:https://bitbucket.org/zunzuncode/CommonProblems - 要查看的文件名为"generateOutput.py",它创建了这些动画。 - James Phillips
太有帮助了 :) 谢谢詹姆斯!我还发现这个网站非常有用,可以将图片制作成GIF:http://gifmaker.me - artDeco
1
太棒了!非常有助于一般理解。我在这里进一步扩展了关于Plotly的内容:https://dev59.com/xHYPtIcB2Jgan1znAKeV#72802696 - Createdd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接