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

时间:2018-07-26 18:57:38

标签: python curve-fitting

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

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以简单地包装已经在函数中编写的代码。也许像这样就足够了:

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

很显然,您可以根据需要更改compute_derivative。 您可以查看以下答案: