3

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.

Drakota
  • 337
  • 1
  • 4
  • 16
  • There is an official matplotlib example about showing the area underneath a curve: [integral_demo](https://matplotlib.org/examples/showcase/integral_demo.html). – ImportanceOfBeingErnest May 16 '17 at 08:59

2 Answers2

3

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()

enter image description here

Vinícius Figueiredo
  • 6,300
  • 3
  • 25
  • 44
1

Scipy can calculate integrals for you, which seems to be the simplest way to get what you're looking for. I think your picture and your function aren't consistent with each other though. Here's what I get plotting your function copy/pasted:

t = pd.Series(range(0,80))
plt.plot(-np.cbrt(np.power(t, 2) - 16 * t + 63) +4)

enter image description here

Anyway here's how to get the integral, given a function and integration bounds.

import scipy.integrate as integrate

# define components for integral calculation
lower_bound = 22
upper_bound = 36

f = lambda t: -np.cbrt(np.power(t, 2) - 16 * t + 63) +4

# calculate integral
integral, error = integrate.quad(f, lower_bound, upper_bound)
print(integral)

-50.03118191324093

Max Power
  • 8,265
  • 13
  • 50
  • 91
  • I didn't show it, but yes the function continues on the right in the negatives. Thanks for the answer! – Drakota May 16 '17 at 03:50
  • Hi Drakota, the issue is a bit different than the function "continuing on the right in the negatives." Look at specific values of f(t) at 20, 30 and 40. In my plot (and Vinicius'), the function is negative at all of those values of t. In your picture the function is positive for all those values. So I don't think you want to present that plot as being of that function – Max Power May 16 '17 at 03:53