Shade Regions with Fill_between

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will learn how to shade regions in a Matplotlib plot using the fill_between function. This is useful for highlighting specific areas of the plot, such as regions where a certain condition is met.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) matplotlib/PlottingDataGroup -.-> matplotlib/fill_between("`Fill Between Plots`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills matplotlib/fill_between -.-> lab-48948{{"`Shade Regions with Fill_between`"}} matplotlib/importing_matplotlib -.-> lab-48948{{"`Shade Regions with Fill_between`"}} matplotlib/figures_axes -.-> lab-48948{{"`Shade Regions with Fill_between`"}} matplotlib/line_plots -.-> lab-48948{{"`Shade Regions with Fill_between`"}} python/tuples -.-> lab-48948{{"`Shade Regions with Fill_between`"}} python/importing_modules -.-> lab-48948{{"`Shade Regions with Fill_between`"}} python/numerical_computing -.-> lab-48948{{"`Shade Regions with Fill_between`"}} python/data_visualization -.-> lab-48948{{"`Shade Regions with Fill_between`"}} end

Import Necessary Libraries

We will start by importing the necessary libraries for this lab, which are numpy and matplotlib.pyplot.

import numpy as np
import matplotlib.pyplot as plt

Create Data

We will create some data to use for our plot. In this example, we will create a sine wave.

t = np.arange(0.0, 2, 0.01)
s = np.sin(2*np.pi*t)

Create the Plot

Now we will create the plot using matplotlib.pyplot. We will plot the sine wave and add a horizontal line at y=0.

fig, ax = plt.subplots()

ax.plot(t, s, color='black')
ax.axhline(0, color='black')

Shade the Regions

We will use fill_between to shade the regions above and below the horizontal line where the sine wave is positive and negative, respectively.

ax.fill_between(t, 1, where=s > 0, facecolor='green', alpha=.5)
ax.fill_between(t, -1, where=s < 0, facecolor='red', alpha=.5)

Show the Plot

Finally, we will show the plot using plt.show().

plt.show()

Summary

In this lab, we learned how to shade regions in a Matplotlib plot using the fill_between function. This is a useful tool for highlighting specific areas of the plot. We hope you found this lab helpful!

Other Python Tutorials you may like