Laura
Laura

Reputation: 11

Calculate area under curve from x,y dataset without even x-step in python

I have a dataset from some CO2 concentration (ppm) values over time, and I would like to know the area under the curve between some time-points.

The most straight-forward solution would be to integrate the function between those time-points, however, I don't know the function, only the values.

Is there any way to integrate the data over a certain period of time directly in python? (without knowing the function) e.g., to calculate the area under the curve between lets say hour 2 and 3 in order to know the amount of CO2 during that period of time

enter image description here

Upvotes: 1

Views: 1858

Answers (1)

Hendrik
Hendrik

Reputation: 31

There is various ways of integrating functions, given only fixed samples. The simplest is probably using the trapezoidal rule. Using numpy, you can do this as follows:

import numpy as np
result = np.trapz([1,2,3], x=[4,6,8])
# result = 8.0

You can find more examples here: https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.trapezoid.html#scipy.integrate.trapezoid

Upvotes: 1

Related Questions