# 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
# 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

def create_graphs_from_file(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()
```

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