8/13
Interactive Plots with Events Β· Page 1 of 1

Event Handling in Plots

Interactive Plots with Events

Making Plots Interactive

While Matplotlib defaults to static plots, you can add event handling for clicks, hovers, and keyboard input.

Basic Event Handling

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1, 2, 3], [1, 4, 2])

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

fig.canvas.mpl_connect('button_press_event', on_click)
plt.show()

Common Event Types

  • button_press_event: Mouse click
  • motion_notify_event: Mouse move (hover)
  • pick_event: Click on plotted object
  • key_press_event: Keyboard

Picking (Highlight Points)

line, = ax.plot([1, 2, 3], [1, 4, 2], picker=True, pickevents=5)

def on_pick(event):
    print(f"Picked point index: {event.ind}")

Note: In QuraLabz, Pyodide has limited interactive capabilities. These patterns work in Jupyter/scripts with display backends.

main.py
Loading...
OUTPUT
β–ΆClick "Run Code" to execute…