Python 如何将导数曲线拟合到同一个图上?

Python 如何将导数曲线拟合到同一个图上?,python,curve-fitting,Python,Curve Fitting,我从一个文本文件的两列中为八个独立的文件绘制了数据图(如下所示)。我需要找到一种方法使其更加自动化,以便可以使用其他数据重新创建。另外,我需要求蓝线的导数,并在同一个图形/轴上绘制(拟合)。哪种方法最好 您只需将已编写的代码包装到函数中即可。也许像这样的事情就足够了: import pylab as plt import numpy as np def compute_derivative(x, y): # if finite differennces are enough

我从一个文本文件的两列中为八个独立的文件绘制了数据图(如下所示)。我需要找到一种方法使其更加自动化,以便可以使用其他数据重新创建。另外,我需要求蓝线的导数,并在同一个图形/轴上绘制(拟合)。哪种方法最好


您只需将已编写的代码包装到函数中即可。也许像这样的事情就足够了:

import pylab as plt
import numpy as np


def compute_derivative(x, y):
    # if finite differennces are enough
    # https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.diff.html
    return x[:-1], np.diff(y)

    # otherwise you can use the gradient function of numpy,
    # with the second argument as the step of your samples
    # take a look here https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.gradient.html
    # return x, np.gradient(y, 0.1)


def create_graphs_from_file(filepath):
    data = np.loadtxt(filepath)
    x = data[:, 0]
    y = -1 * data[:, 1]
    z = data[:, 3]

    derivative_x, derivative_y = compute_derivative(x, y)

    fig_title = '{}: Length = 100, Width = 100'.format(filepath)
    plt.figure(fig_title)
    plt.title(fig_title)
    plt.plot(x, y, color='b', label='Drain Current')
    plt.plot(x, z, color='r', label='Leak Current')
    plt.plot(derivative_x, derivative_y, color='g', label='Derivative of the Drain Current')
    plt.xlabel(r'$V_G')
    plt.ylabel(r'$I_{DS}')
    plt.legend(loc=1)


if __name__ == '__main__':
    # list with filepaths of the files you want to plot
    files_to_plot = [
        'filepath1',
        'filepath2',
        'filepath3',
    ]

    for f in files_to_plot:
        create_graphs_from_file(f)

    plt.show()
显然,您可以根据需要更改
计算导数。
您可以查看以下答案:


(1)如果您愿意,请仅针对data01.txt文件发布代码和数据链接,因为从屏幕截图中键入代码有点繁琐且容易出错。(2) 您使用什么函数来拟合数据?(3) 你考虑过一个z=f(x,y)类型的3D曲面方程来拟合数据吗?我尝试过使用这种方法,但在尝试运行它时,似乎什么都没有出现。你如何运行它?您是否已使用数据文件的路径将
文件\u改编为\u plot
列表?