Home > Enterprise >  How to calculate the area of the positive part of a graph in python?
How to calculate the area of the positive part of a graph in python?

Time:05-30

I have a dataframe

>>print(df)
                          Power
timestamp                      
2019-02-16 00:00:00  -7240.2360
2019-02-16 01:00:00  -7598.0856
2019-02-16 02:00:00  -7563.9708
2019-02-16 03:00:00  -7247.5380
2019-02-16 04:00:00  -7167.5292
2019-02-16 05:00:00  -7540.6572
2019-02-16 06:00:00  -8856.7836
2019-02-16 07:00:00  -5313.8868
2019-02-16 08:00:00   1019.7360
2019-02-16 09:00:00    853.9788
2019-02-16 10:00:00  24312.1020
2019-02-16 11:00:00  45792.9588
2019-02-16 12:00:00  48332.9124
2019-02-16 13:00:00  40819.7460
2019-02-16 14:00:00  46905.9012
2019-02-16 15:00:00  33089.8188
2019-02-16 16:00:00  38144.0256
2019-02-16 17:00:00  20626.7040
2019-02-16 18:00:00   4575.4176
2019-02-16 19:00:00  -5906.9352
2019-02-16 20:00:00  -8736.6144
2019-02-16 21:00:00  -8649.7056
2019-02-16 22:00:00  -7612.5216
2019-02-16 23:00:00  -7432.0824

If I plot them I have:

df.plot()

enter image description here

Based on the comment:

s = [0,5,-1,0,0,-1,5,0]
s = [x for x in s if x>=0]
plt.plot(s)
np.trapz(s)

Output:

enter image description here

  • Related