Mouse Interaction with Matplotlib Plot

PythonPythonBeginner
Practice Now

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

Introduction

This lab demonstrates an example of how to interact with the plotting canvas by connecting to move and click events using Matplotlib library in Python. Matplotlib is a data visualization library that allows users to create static, animated, and interactive visualizations in Python.

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.

Creating a Sine Wave Plot

First, we need to create a sine wave plot using numpy and matplotlib libraries.

import matplotlib.pyplot as plt
import numpy as np

t = np.arange(0.0, 1.0, 0.01)
s = np.sin(2 * np.pi * t)
fig, ax = plt.subplots()
ax.plot(t, s)

Mouse Move Event

We can connect to mouse move events using the motion_notify_event method. In this example, we are printing the x and y data coordinates and x and y pixel coordinates when the mouse moves over the plot.

def on_move(event):
    if event.inaxes:
        print(f'data coords {event.xdata} {event.ydata},',
              f'pixel coords {event.x} {event.y}')

binding_id = plt.connect('motion_notify_event', on_move)

Mouse Click Event

We can connect to mouse click events using the button_press_event method. In this example, we are disconnecting the mouse move event callback when the left mouse button is clicked.

from matplotlib.backend_bases import MouseButton

def on_click(event):
    if event.button is MouseButton.LEFT:
        print('disconnecting callback')
        plt.disconnect(binding_id)

plt.connect('button_press_event', on_click)

Displaying the Plot

Finally, we need to display the plot using the show method.

plt.show()

Summary

This lab demonstrated how to interact with a Matplotlib plot using mouse move and click events. By connecting to these events, we can perform various actions like printing the coordinates of the mouse pointer, disconnecting callbacks, etc. This technique can be useful to create interactive visualizations in Python.

Other Python Tutorials you may like