How to fit the curve of the derivative onto the same graph? [closed]

I graphed data (shown below) from two columns of a text file for eight separate files. I need to find a way to make this more automatic so that can be recreated with other data. Also, I need to take the derivative of the blue line, and graph (fit) on the same graph/axis. What method would be best to do so?

enter image description here

enter image description here


You can simply wrap the code you’ve already written in a function. Maybe something like this could be enough:

import pylab as plt
import numpy as np

def compute_derivative(x, y):
    # if finite differennces are enough
    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
    # 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.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')

if __name__ == '__main__':
    # list with filepaths of the files you want to plot
    files_to_plot = [

    for f in files_to_plot:

Obviously you can change compute_derivative with whatever you want. You can take a look to these answers:

Leave a Reply

Your email address will not be published. Required fields are marked *