I have Pandas DataFrame with 2 columns 'x', 'y':
tmp = pd.DataFrame()
tmp['x'] = [1, 2, 5, 9, 12, 14]
tmp['y'] = [0, 1, -2, 2, -1, 1]
tmp.plot(x = 'x', y = 'y')
ax = plt.gca()
ax.set_aspect('equal')
ax.grid(True, which='both')
ax.axhline(y=0, color='k')
ax.axvline(x=0, color='k')
plt.show()
Here is the plot:
I want to get areas of triangles which are above y=0 and below y=0 separately.
Is there an easy way to do this?
I've tried to use integration, but seems I'm doing something wrong:
pos = tmp[tmp['y']>=0]
neg = tmp[tmp['y']<=0]
print(integrate.trapezoid(pos['y'], pos['x'])) # this gives 18.5 instead of desired 5.5
print(integrate.trapezoid(neg['y'], neg['x'])) # this gives -14.5 instead of desired 5
# seems this does correct calculation, but calculates areas as negative and positive and in total it gives small area
print(integrate.trapezoid(tmp['y'], tmp['x'])) # this gives 0.5 instead of desired 10.5
This is a smaller/simpler code to show what I want to do. Actually my data is quite large. I think I can achieve desired results by adding corresponding (x, y) values where y = 0, but I'm wondering if there are existing functions that will do similar job.