Using Fill Between to Fill the Area between Two Curves

In this lesson, we will learn how to use fill_between to fill the area between two curves.

Sometimes, we want to fill the areas between two curves, especially when those areas are in different quadrants, and we want to fill them with different colors. For example, when drawing a sin(x)sin(x) curve, we may want the top and the bottom of the x-axis to be filled with different colors.

As another, more practical example, when we fit some data points with a line, we may want to draw a confidence region for our line, similar to how we might use the errorbar plot.

In order to draw a confidence region, the basic function to call is fill_between(). Below are some of the important parameters required by the function:

  • x: The x coordinates of the nodes defining the curves.
  • y1: The y coordinates of the nodes defining the first curve.
  • y2: The y coordinates of the nodes defining the second curve.
  • where: Array of bool. Defines where to exclude some horizontal regions from being filled. For example, fill between x[i]x[i] and x[i+1]x[i+1] if where[i]where[i] and where[i+1]where[i+1] is True.

Get hands-on with 1200+ tech skills courses.