How to find the area below a function in matplotlib?

Viewed 4321

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

Image

I wish to get the area of this zone

Image

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.

2 Answers
Related