Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to shade region under the curve in matplotlib

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)) 
like image 720
lukecampbell Avatar asked Apr 06 '12 16:04

lukecampbell


People also ask

How do I shade an area in matplotlib?

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.

How do I fill a area in matplotlib?

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.

How do you fill an area between two lines in python?

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.


1 Answers

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)) 
like image 124
lukecampbell Avatar answered Sep 21 '22 13:09

lukecampbell