Creating a Looking Glass With Matplotlib

PythonPythonBeginner
Practice Now

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

Introduction

This lab will guide you through creating a looking glass effect using mouse events in Matplotlib. This example will allow you to inspect data in a circular area that can be moved around by clicking and dragging.

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/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/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) 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/AdvancedTopicsGroup -.-> matplotlib/event_handling("`Event Handling`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/constructor("`Constructor`") python/ObjectOrientedProgrammingGroup -.-> python/polymorphism("`Polymorphism`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills python/comments -.-> lab-48814{{"`Creating a Looking Glass With Matplotlib`"}} matplotlib/importing_matplotlib -.-> lab-48814{{"`Creating a Looking Glass With Matplotlib`"}} matplotlib/figures_axes -.-> lab-48814{{"`Creating a Looking Glass With Matplotlib`"}} matplotlib/line_plots -.-> lab-48814{{"`Creating a Looking Glass With Matplotlib`"}} matplotlib/event_handling -.-> lab-48814{{"`Creating a Looking Glass With Matplotlib`"}} python/conditional_statements -.-> lab-48814{{"`Creating a Looking Glass With Matplotlib`"}} python/for_loops -.-> lab-48814{{"`Creating a Looking Glass With Matplotlib`"}} python/lists -.-> lab-48814{{"`Creating a Looking Glass With Matplotlib`"}} python/tuples -.-> lab-48814{{"`Creating a Looking Glass With Matplotlib`"}} python/function_definition -.-> lab-48814{{"`Creating a Looking Glass With Matplotlib`"}} python/importing_modules -.-> lab-48814{{"`Creating a Looking Glass With Matplotlib`"}} python/standard_libraries -.-> lab-48814{{"`Creating a Looking Glass With Matplotlib`"}} python/classes_objects -.-> lab-48814{{"`Creating a Looking Glass With Matplotlib`"}} python/constructor -.-> lab-48814{{"`Creating a Looking Glass With Matplotlib`"}} python/polymorphism -.-> lab-48814{{"`Creating a Looking Glass With Matplotlib`"}} python/encapsulation -.-> lab-48814{{"`Creating a Looking Glass With Matplotlib`"}} python/math_random -.-> lab-48814{{"`Creating a Looking Glass With Matplotlib`"}} python/numerical_computing -.-> lab-48814{{"`Creating a Looking Glass With Matplotlib`"}} python/data_visualization -.-> lab-48814{{"`Creating a Looking Glass With Matplotlib`"}} end

Importing the Required Libraries

We need to import the Matplotlib library, NumPy library and the Matplotlib patches module. We will use these libraries to create our looking glass effect.

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

Generating the Random Data

We will generate two sets of random data using NumPy. This data will be plotted to create a scatterplot.

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

x, y = np.random.rand(2, 200)

Creating the Figure and Axes

We will create the figure and axes object using the subplots() function. We will also add a yellow circle patch to the axes object using the patches.Circle() function.

fig, ax = plt.subplots()
circ = patches.Circle((0.5, 0.5), 0.25, alpha=0.8, fc='yellow')
ax.add_patch(circ)

Plotting the Data

We will plot the random data generated in Step 2 using the plot() function twice. The first plot will have an alpha value of 0.2 and the second plot will have an alpha value of 1.0 and a clip path set to the yellow circle patch.

ax.plot(x, y, alpha=0.2)
line, = ax.plot(x, y, alpha=1.0, clip_path=circ)
ax.set_title("Left click and drag to move looking glass")

Creating the Event Handler

We will create an event handler class that will handle the mouse events needed to move the yellow circle patch around the plot. This class will contain three methods: on_press(), on_release(), and on_move().

class EventHandler:
    def __init__(self):
        fig.canvas.mpl_connect('button_press_event', self.on_press)
        fig.canvas.mpl_connect('button_release_event', self.on_release)
        fig.canvas.mpl_connect('motion_notify_event', self.on_move)
        self.x0, self.y0 = circ.center
        self.pressevent = None

    def on_press(self, event):
        if event.inaxes != ax:
            return

        if not circ.contains(event)[0]:
            return

        self.pressevent = event

    def on_release(self, event):
        self.pressevent = None
        self.x0, self.y0 = circ.center

    def on_move(self, event):
        if self.pressevent is None or event.inaxes != self.pressevent.inaxes:
            return

        dx = event.xdata - self.pressevent.xdata
        dy = event.ydata - self.pressevent.ydata
        circ.center = self.x0 + dx, self.y0 + dy
        line.set_clip_path(circ)
        fig.canvas.draw()

handler = EventHandler()
plt.show()

Running the Program

Run the program and left-click and drag the yellow circle patch to move it around the plot. You can use this effect to inspect data within the circular area.

Summary

In this lab, we learned how to create a looking glass effect using mouse events in Matplotlib. We generated random data, plotted the data, and created an event handler class to handle mouse events. By left-clicking and dragging the yellow circle patch, we were able to move it around the plot and inspect data within the circular area.

Other Python Tutorials you may like