Search code examples
pythonpandasnumpyscipyderivative

Extract and plot the first derivative of a curve in python without knowing its formula?


I have one curve that I plot from a dataframe, I would like to extract the first derivative of the curve but I don't actually know its formula. After having the first derivative, I would like to extract the point where it reaches half of its maximum value.

Is this possible to do in python?



      X           y
0   -0.000131   0.006983
1   0.013764    0.006666
2   0.028727    0.007242
3   0.137593    0.007117
4   0.538855    0.007305
5   0.963937    0.008790
6   1.318019    0.011174
7   1.569953    0.012909
8   1.751192    0.016706
9   1.957168    0.020321
10  2.125429    0.028622
11  2.297202    0.035952
12  2.483939    0.046041
13  2.663194    0.056283
14  2.855580    0.068497
15  3.043233    0.082118
16  3.243100    0.097764
17  3.437318    0.111928
18  3.639782    0.129328
19  3.839039    0.145694
20  4.044403    0.163474
21  4.243966    0.182325
22  4.453453    0.200748
23  4.654542    0.218878
24  4.868304    0.238715
25  5.070615    0.254610
26  5.288194    0.272639
27  5.494627    0.287299
28  5.723200    0.303031
29  5.931618    0.315398
30  6.189813    0.316119
31  6.384031    0.328265
32  6.658715    0.327107
33  6.855071    0.333307
34  7.138154    0.331096
35  7.334967    0.338243
36  7.625989    0.335927
37  7.820513    0.343463
38  8.077639    0.339921
39  8.264375    0.349140
40  8.473862    0.344021
41  8.652201    0.349255
42  8.816034    0.346376
43  8.944750    0.345141
44  9.079573    0.344968
45  9.130418    0.344184
46  9.255163    0.342439
47  9.195768    0.345535
48  9.247223    0.344030
49  9.192867    0.345617
50  9.281273    0.337575

enter image description here


Solution

  • You can use df.diff().eval('y/X') to compute the derivative:

    ax = df.plot(x='X', y='y')
    df = df.assign(derivative=df.diff().eval('y/X'))
    df.plot(x='X', y='derivative', ax=ax)
    

    curve + derivative