Drawing Shapes with Matplotlib Widgets

PythonPythonBeginner
Practice Now

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

Introduction

In this lab, we will learn how to use the RectangleSelector and EllipseSelector widgets in Matplotlib to draw a rectangle or ellipse from the initial click position to the current mouse position.

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 python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/InteractiveFeaturesGroup(["`Interactive Features`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/AdvancedTopicsGroup(["`Advanced Topics`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") matplotlib/InteractiveFeaturesGroup -.-> matplotlib/interactive_backends("`Interactive Backends`") matplotlib/InteractiveFeaturesGroup -.-> matplotlib/widgets_sliders("`Widgets and Sliders`") matplotlib/AdvancedTopicsGroup -.-> matplotlib/event_handling("`Event Handling`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/ControlFlowGroup -.-> python/list_comprehensions("`List Comprehensions`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/DataStructuresGroup -.-> python/sets("`Sets`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-48904{{"`Drawing Shapes with Matplotlib Widgets`"}} matplotlib/importing_matplotlib -.-> lab-48904{{"`Drawing Shapes with Matplotlib Widgets`"}} matplotlib/figures_axes -.-> lab-48904{{"`Drawing Shapes with Matplotlib Widgets`"}} matplotlib/line_plots -.-> lab-48904{{"`Drawing Shapes with Matplotlib Widgets`"}} matplotlib/interactive_backends -.-> lab-48904{{"`Drawing Shapes with Matplotlib Widgets`"}} matplotlib/widgets_sliders -.-> lab-48904{{"`Drawing Shapes with Matplotlib Widgets`"}} matplotlib/event_handling -.-> lab-48904{{"`Drawing Shapes with Matplotlib Widgets`"}} python/booleans -.-> lab-48904{{"`Drawing Shapes with Matplotlib Widgets`"}} python/conditional_statements -.-> lab-48904{{"`Drawing Shapes with Matplotlib Widgets`"}} python/for_loops -.-> lab-48904{{"`Drawing Shapes with Matplotlib Widgets`"}} python/list_comprehensions -.-> lab-48904{{"`Drawing Shapes with Matplotlib Widgets`"}} python/lists -.-> lab-48904{{"`Drawing Shapes with Matplotlib Widgets`"}} python/tuples -.-> lab-48904{{"`Drawing Shapes with Matplotlib Widgets`"}} python/dictionaries -.-> lab-48904{{"`Drawing Shapes with Matplotlib Widgets`"}} python/sets -.-> lab-48904{{"`Drawing Shapes with Matplotlib Widgets`"}} python/function_definition -.-> lab-48904{{"`Drawing Shapes with Matplotlib Widgets`"}} python/importing_modules -.-> lab-48904{{"`Drawing Shapes with Matplotlib Widgets`"}} python/using_packages -.-> lab-48904{{"`Drawing Shapes with Matplotlib Widgets`"}} python/numerical_computing -.-> lab-48904{{"`Drawing Shapes with Matplotlib Widgets`"}} python/data_visualization -.-> lab-48904{{"`Drawing Shapes with Matplotlib Widgets`"}} python/build_in_functions -.-> lab-48904{{"`Drawing Shapes with Matplotlib Widgets`"}} end

Import necessary libraries

We will start by importing the necessary libraries: Matplotlib, Numpy, and the RectangleSelector and EllipseSelector widgets.

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import EllipseSelector, RectangleSelector

Define the select callback function

The select callback function will be called every time the user selects a rectangle or ellipse. The function will receive the click and release events as arguments and print out the coordinates of the rectangle or ellipse.

def select_callback(eclick, erelease):
    """
    Callback for line selection.

    *eclick* and *erelease* are the press and release events.
    """
    x1, y1 = eclick.xdata, eclick.ydata
    x2, y2 = erelease.xdata, erelease.ydata
    print(f"({x1:3.2f}, {y1:3.2f}) --> ({x2:3.2f}, {y2:3.2f})")
    print(f"The buttons you used were: {eclick.button} {erelease.button}")

Define the toggle selector function

The toggle selector function will be called every time the user presses the 't' key. This function will toggle the active status of the RectangleSelector and EllipseSelector widgets.

def toggle_selector(event):
    print('Key pressed.')
    if event.key == 't':
        for selector in selectors:
            name = type(selector).__name__
            if selector.active:
                print(f'{name} deactivated.')
                selector.set_active(False)
            else:
                print(f'{name} activated.')
                selector.set_active(True)

Create the figure and subplots

We will create a figure with two subplots, one for the RectangleSelector and one for the EllipseSelector.

fig = plt.figure(layout='constrained')
axs = fig.subplots(2)

Plot something on the subplots

We will plot something on the subplots, so that the user can see the effect of the RectangleSelector and EllipseSelector.

N = 100000  ## If N is large one can see improvement by using blitting.
x = np.linspace(0, 10, N)

for ax in axs:
    ax.plot(x, np.sin(2*np.pi*x))  ## plot something

Create the RectangleSelector and EllipseSelector widgets

We will create the RectangleSelector and EllipseSelector widgets and add them to the subplots.

selectors = []
for ax, selector_class in zip(axs, [RectangleSelector, EllipseSelector]):
    ax.set_title(f"Click and drag to draw a {selector_class.__name__}.")
    selectors.append(selector_class(
        ax, select_callback,
        useblit=True,
        button=[1, 3],  ## disable middle button
        minspanx=5, minspany=5,
        spancoords='pixels',
        interactive=True))
    fig.canvas.mpl_connect('key_press_event', toggle_selector)
axs[0].set_title("Press 't' to toggle the selectors on and off.\n"
                 + axs[0].get_title())

Show the plot

Finally, we will show the plot to the user.

plt.show()

Summary

In this lab, we learned how to use the RectangleSelector and EllipseSelector widgets in Matplotlib to draw a rectangle or ellipse from the initial click position to the current mouse position. We also learned how to create a figure with subplots, plot something on the subplots, and toggle the active status of the widgets using a key press event.

Other Python Tutorials you may like