I'm new to python and the library matplotlib, I'm trying to get the area below my function line in my plot. I have a variable a & b that moves a rectangle in my plot. I could probably use raw math to solve this problem, but I wanted to know if there was a simpler way to achieve what I'm trying to do with matplotlib.
My plot looks like this

I wish to get the area of this zone

If, there's also an easy way to color this area, I'd like to hear it too.
Here's my code to display this plot :
plt.clf()
plt.draw()
plt.axis(xmin = 0, xmax = 80, ymin = 0, ymax = 5)
plt.plot(-np.cbrt(np.power(t, 2) - 16 * t + 63) +4)
currentAxis = plt.gca()
line = currentAxis.lines[0]
for x, y in zip(line.get_xdata(), line.get_ydata()):
if x == 0 or y == 0:
if b > a:
currentAxis.add_patch(patches.Rectangle(((a * 80 / 11), y), (b * 80 / 11) - (a * 80 / 11), 5, fill=False))
else:
currentAxis.add_patch(patches.Rectangle((-(b * 80 / 11) + 80, y), -(a * 80 / 11) + (b * 80 / 11), 5, fill=False))
plt.show()
Thanks for helping, sorry for no embed images.
Since Max Power's answer to the integration part of your question is really good, I'll just be adressing the issue of drawing the area below/above curve. You can use fill_between:
import numpy as np
from matplotlib import pyplot as plt
def f(t):
return -np.cbrt(np.power(t, 2) - 16 * t + 63) +4
t = np.arange(0,80,1/40.)
plt.plot(t,f(t))
section = np.arange(22, 36, 1/20.)
plt.fill_between(section,f(section))
plt.axhline(0, color='k')
plt.axvline(0, color='k')
plt.show()

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With