Matplotlib Span Selector

PythonPythonBeginner
Practice Now

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

Introduction

This lab will guide you through how to use the Matplotlib Span Selector to select a range on an axis and plot a detailed view of the selected range on another axis.

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.

Import Required Libraries

First, we need to import the required libraries - numpy and matplotlib.

import numpy as np
import matplotlib.pyplot as plt

Create Sample Data

We will now create some sample data to plot using numpy.

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

x = np.arange(0.0, 5.0, 0.01)
y = np.sin(2 * np.pi * x) + 0.5 * np.random.randn(len(x))

Create Figure and Subplots

We will now create a figure with two subplots using matplotlib.

fig, (ax1, ax2) = plt.subplots(2, figsize=(8, 6))

Plot Data on First Subplot

We will plot the sample data on the first subplot.

ax1.plot(x, y)
ax1.set_ylim(-2, 2)
ax1.set_title('Press left mouse button and drag '
              'to select a region in the top graph')

Define a Callback Function

We will define a callback function that will be called when a range is selected using the Span Selector.

def onselect(xmin, xmax):
    indmin, indmax = np.searchsorted(x, (xmin, xmax))
    indmax = min(len(x) - 1, indmax)

    region_x = x[indmin:indmax]
    region_y = y[indmin:indmax]

    if len(region_x) >= 2:
        line2.set_data(region_x, region_y)
        ax2.set_xlim(region_x[0], region_x[-1])
        ax2.set_ylim(region_y.min(), region_y.max())
        fig.canvas.draw_idle()

Create a Span Selector

We will create a Span Selector object using matplotlib.widgets.SpanSelector.

span = SpanSelector(
    ax1,
    onselect,
    "horizontal",
    useblit=True,
    props=dict(alpha=0.5, facecolor="tab:blue"),
    interactive=True,
    drag_from_anywhere=True
)

Plot Data on Second Subplot

We will plot the detailed view of the selected range on the second subplot.

line2, = ax2.plot([], [])

Show Plot

We will now show the plot using matplotlib.pyplot.show().

plt.show()

Summary

In this lab, we learned how to use the Matplotlib Span Selector to select a range on an axis and plot a detailed view of the selected range on another axis. We also learned how to create a Span Selector object and define a callback function to handle the selected range.

Other Python Tutorials you may like