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))
To shade an area parallel to X-axis, initialize two variables, y1 and y2. To add horizontal span across the axes, use axhspan() method with y1, y2, green as shade color,and alpha for transprency of the shade. To display the figure, use show() method.
fill_between() is used to fill area between two horizontal curves. Two points (x, y1) and (x, y2) define the curves. this creates one or more polygons describing the filled areas.
You can easily fill in the area between values in a Matplotlib plot by using following functions: fill_between(): Fill the area between two horizontal curves. fill_betweenx(): Fill the area between two vertical curves.
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))
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