0
0
Matplotlibdata~5 mins

Cursor and event handling in Matplotlib

Choose your learning style9 modes available
Introduction

Cursor and event handling lets you interact with plots. You can respond when someone moves the mouse or clicks on the graph.

You want to show data values when the mouse moves over a plot.
You want to let users click on points to get more info.
You want to change the plot based on mouse actions.
You want to create interactive visualizations for presentations.
You want to track mouse position on a graph for analysis.
Syntax
Matplotlib
fig, ax = plt.subplots()

# Define an event handler function
def on_move(event):
    if event.inaxes:
        print(f"Mouse at x={event.xdata:.2f}, y={event.ydata:.2f}")

# Connect the event to the handler
cid = fig.canvas.mpl_connect('motion_notify_event', on_move)

plt.show()

Use mpl_connect to link events like mouse move or click to your function.

The event object gives info like mouse position inside the plot.

Examples
This example prints the mouse coordinates as you move inside the plot area.
Matplotlib
fig, ax = plt.subplots()

# Print mouse position when moved inside plot

def on_move(event):
    if event.inaxes:
        print(f"Mouse at x={event.xdata:.1f}, y={event.ydata:.1f}")

fig.canvas.mpl_connect('motion_notify_event', on_move)
plt.show()
This example prints the coordinates where you click inside the plot.
Matplotlib
fig, ax = plt.subplots()

# Print message when mouse clicked

def on_click(event):
    if event.inaxes:
        print(f"Clicked at x={event.xdata:.2f}, y={event.ydata:.2f}")

fig.canvas.mpl_connect('button_press_event', on_click)
plt.show()
This example shows a red crosshair cursor that moves with the mouse on the plot.
Matplotlib
fig, ax = plt.subplots()

# Show a crosshair cursor following the mouse

cursor = Cursor(ax, useblit=True, color='red', linewidth=1)

plt.show()
Sample Program

This program plots points and shows a green crosshair cursor. When you click inside the plot, it prints the clicked coordinates.

Matplotlib
import matplotlib.pyplot as plt
from matplotlib.widgets import Cursor

fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4], [10, 20, 25, 30], marker='o')

# Create a cursor that follows the mouse
cursor = Cursor(ax, useblit=True, color='green', linewidth=1)

# Define event handler for mouse clicks
def on_click(event):
    if event.inaxes == ax:
        print(f"Clicked at x={event.xdata:.2f}, y={event.ydata:.2f}")

# Connect the click event to the handler
fig.canvas.mpl_connect('button_press_event', on_click)

plt.title('Click on points to see coordinates')
plt.show()
OutputSuccess
Important Notes

Event handlers must check if the event happened inside the plot area using event.inaxes.

The Cursor widget helps show a crosshair cursor easily.

Use mpl_connect to link many event types like mouse move, click, key press.

Summary

Cursor and event handling make plots interactive and responsive to mouse actions.

Use mpl_connect to connect mouse events to your functions.

The Cursor widget shows a crosshair that follows the mouse.