How to shade region under the curve in matplotlib


Question

I want to use matplotlib to illustrate the definite integral between two regions: x_0, and x_1.

How can I shade a region under a curve in matplotlib from x=-1, to x=1 given the following plot

import numpy as np
from matplotlib import pyplot as plt
def f(t):
    return t * t

t = np.arange(-4,4,1/40.)
plt.plot(t,f(t))
1
27
10/2/2018 12:06:31 PM

Accepted Answer

Check out fill. Here's an example on filling a constrained region.

16
3/31/2016 8:57:39 AM

The final answer I came up with is to use fill_between.

I thought there would have been a simple shade between type method, but this does exactly what I want.

section = np.arange(-1, 1, 1/20.)
plt.fill_between(section,f(section))

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon