Search code examples
pythonpandasdataframenumpyarea

Area between two curves


I was able to plot the lines and fill the area in between, however, I'm trying to calculate the area between the two curves. I cant use the integral since I don't have the equation, I only have a bunch of points. How can I calculate the area in between?

fig,ax1= plt.subplots(figsize=(8,5))
plt.plot(Lag_Distance,True_Values,color='blue',label='Sampled Variogram'); plt.scatter(Lag_Distance,True_Values,color='blue',edgecolor='blue',zorder=10)
plt.plot(Lag_Distance,a['Variogram Value'],color='black',label='Estimated Variogram (P=1)'); plt.scatter(Lag_Distance,a['Variogram Value'],color='black',edgecolor='black',zorder=10) 
plt.fill_between(Lag_Distance,a['Variogram Value'],True_Values,color='red',alpha=0.2)
plt.legend(loc='lower right',fontsize=20)
ax1.set_xlabel('Lag Distance',fontsize=20);
ax1.set_ylabel('$^\gamma$',fontsize=30);
ax1.set_title(' Variogram in the major direction 160 (P =1) ',fontsize=20);
plt.subplots_adjust(left=0.0,bottom=0.0,right=1.0,top=1.1); plt.show() # set plot size

enter image description here


Solution

  • Since all of the points are connected by lines, integrating using the trapezoid rule will give you the exact area.

    The numpy library has a trapezoidal integration function so you can take the difference between the area under the estimated variogram and the area under the sampled variogram:

    import numpy as np
    
    area = np.trapz(y=a['Variogram Value'] - True_Values, x=Lag_Distance)