Matplotlib Fill Between and Alpha

PythonPythonBeginner
Practice Now

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

Introduction

In data visualization, sometimes it is necessary to highlight certain areas or ranges on a graph. The fill_between function of Matplotlib is a useful tool for generating a shaded region between a minimum and maximum boundary. It can also be used to enhance the visual appearance of a graph. The alpha argument can be used to adjust the transparency of the shaded region. This lab will guide you through several examples of using fill_between and alpha in Matplotlib to create more visually appealing and informative graphs.

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.

Enhancing a Line Plot with fill_between

The first example demonstrates how to enhance a line plot with fill_between. We will use financial data from Google to create two subplots, one with a simple line plot and the other with a filled line plot.

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.cbook as cbook

## load up some sample financial data
r = cbook.get_sample_data('goog.npz')['price_data'].view(np.recarray)

## create two subplots with the shared x and y axes
fig, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True)

pricemin = r.close.min()

ax1.plot(r.date, r.close, lw=2)
ax2.fill_between(r.date, pricemin, r.close, alpha=0.7)

for ax in ax1, ax2:
    ax.grid(True)
    ax.label_outer()

ax1.set_ylabel('price')
fig.suptitle('Google (GOOG) daily closing price')
fig.autofmt_xdate()

Using alpha to Soften Colors

The alpha argument can also be used to soften colors for more visually appealing plots. In the following example, we will compute two populations of random walkers with a different mean and standard deviation of the normal distributions from which the steps are drawn. We use filled regions to plot +/- one standard deviation of the mean position of the population.

## Fixing random state for reproducibility
np.random.seed(19680801)

Nsteps, Nwalkers = 100, 250
t = np.arange(Nsteps)

## an (Nsteps x Nwalkers) array of random walk steps
S1 = 0.004 + 0.02*np.random.randn(Nsteps, Nwalkers)
S2 = 0.002 + 0.01*np.random.randn(Nsteps, Nwalkers)

## an (Nsteps x Nwalkers) array of random walker positions
X1 = S1.cumsum(axis=0)
X2 = S2.cumsum(axis=0)

## Nsteps length arrays empirical means and standard deviations of both
## populations over time
mu1 = X1.mean(axis=1)
sigma1 = X1.std(axis=1)
mu2 = X2.mean(axis=1)
sigma2 = X2.std(axis=1)

## plot it!
fig, ax = plt.subplots(1)
ax.plot(t, mu1, lw=2, label='mean population 1')
ax.plot(t, mu2, lw=2, label='mean population 2')
ax.fill_between(t, mu1+sigma1, mu1-sigma1, facecolor='C0', alpha=0.4)
ax.fill_between(t, mu2+sigma2, mu2-sigma2, facecolor='C1', alpha=0.4)
ax.set_title(r'random walkers empirical $\mu$ and $\pm \sigma$ interval')
ax.legend(loc='upper left')
ax.set_xlabel('num steps')
ax.set_ylabel('position')
ax.grid()

Highlighting Certain Regions with where

The where keyword argument is very handy for highlighting certain regions of the graph. where takes a boolean mask the same length as the x, ymin and ymax arguments, and only fills in the region where the boolean mask is True. In the example below, we simulate a single random walker and compute the analytic mean and standard deviation of the population positions. The population mean is shown as the dashed line, and the plus/minus one sigma deviation from the mean is shown as the filled region. We use the where mask X > upper_bound to find the region where the walker is outside the one sigma boundary, and shade that region red.

## Fixing random state for reproducibility
np.random.seed(1)

Nsteps = 500
t = np.arange(Nsteps)

mu = 0.002
sigma = 0.01

## the steps and position
S = mu + sigma*np.random.randn(Nsteps)
X = S.cumsum()

## the 1 sigma upper and lower analytic population bounds
lower_bound = mu*t - sigma*np.sqrt(t)
upper_bound = mu*t + sigma*np.sqrt(t)

fig, ax = plt.subplots(1)
ax.plot(t, X, lw=2, label='walker position')
ax.plot(t, mu*t, lw=1, label='population mean', color='C0', ls='--')
ax.fill_between(t, lower_bound, upper_bound, facecolor='C0', alpha=0.4,
                label='1 sigma range')
ax.legend(loc='upper left')

## here we use the where argument to only fill the region where the
## walker is above the population 1 sigma boundary
ax.fill_between(t, upper_bound, X, where=X > upper_bound, fc='red', alpha=0.4)
ax.fill_between(t, lower_bound, X, where=X < lower_bound, fc='red', alpha=0.4)
ax.set_xlabel('num steps')
ax.set_ylabel('position')
ax.grid()

Highlighting Spans of an Axes with axhspan and axvspan

Another handy use of filled regions is to highlight horizontal or vertical spans of an Axes. For that Matplotlib has the helper functions axhspan and axvspan. See the axhspan_demo gallery for more information.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.axhspan(0.25, 0.75, facecolor='0.5', alpha=0.5)
ax.axvspan(6, 7, facecolor='r', alpha=0.5)

plt.show()

Summary

In this lab, we learned how to use the fill_between function and the alpha argument in Matplotlib to create more visually appealing and informative graphs. We demonstrated several examples of using fill_between and alpha to highlight certain regions or ranges of a graph. We also briefly introduced the axhspan and axvspan functions for highlighting spans of an Axes.

Other Python Tutorials you may like