Interactive Event Handling

Event handling lets a Matplotlib figure react to the user — connecting callback functions that fire on mouse clicks, mouse motion, and key presses so a static chart becomes an interactive tool.

Learn Interactive Event Handling in our free Matplotlib course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free Matplotlib course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll connect callbacks with fig.canvas.mpl_connect() , read click positions from event.xdata / event.ydata , handle key presses, and pick the artist a user clicks.

The hub of interactivity is fig.canvas.mpl_connect(event_name, callback) . You name the event you care about and hand it a function. Matplotlib calls that function with an event object each time it happens. For a click the event is 'button_press_event' , and the click position in data units is event.xdata , event.ydata .

What you'll see here: an empty 10×10 grid and a printed confirmation that the handler is connected. Run it locally and every click drops a red dot exactly where you pressed.

The same mpl_connect pattern handles other events. 'key_press_event' gives you event.key (the character or name like 'up' or 'enter' ), and 'motion_notify_event' fires continuously as the mouse moves, so you can show a live readout of the cursor position.

Sometimes you do not just want the click position — you want to know which point or line was clicked. Plot with picker=True (or a pixel tolerance) and connect to 'pick_event' . The callback receives event.artist (what was picked) and event.ind (the indices of the nearest data points).

What you'll see here: a static scatter of 25 points and a printed confirmation. In a live window, clicking near any point prints its index and coordinates and updates the title.

Replace each ___ to connect a click handler that prints where you clicked.

You are on a non-interactive backend. ✅ Run locally with a GUI backend (TkAgg/Qt); inline or headless backends never deliver events.

The click was outside the axes, so xdata is None . ✅ Guard with if event.inaxes: before using the coordinates.

You changed the data but did not refresh. ✅ Call fig.canvas.draw() or draw_idle() at the end of the callback.

Build a figure whose title counts how many times the user has clicked inside the axes.

Lesson complete — your figures can listen!

You connected callbacks with mpl_connect, handled clicks, key presses, and mouse motion, read positions from event.xdata/ydata, and picked the artist a user clicked.

🚀 Up next: Managing Multiple Figures — juggle several figure windows, switch the current one, and close them cleanly.

Practice quiz

Which method connects a callback to figure events?

  • fig.canvas.mpl_connect()
  • fig.on_event()
  • ax.connect()
  • plt.bind()

Answer: fig.canvas.mpl_connect(). fig.canvas.mpl_connect(event_name, callback) wires a function to a named event.

Which event name fires when a mouse button is pressed?

  • click_event
  • button_press_event
  • mouse_down
  • press_event

Answer: button_press_event. 'button_press_event' fires every time a mouse button is pressed inside the figure.

Inside a callback, where is the click position in data coordinates?

  • event.x and event.y (pixels)
  • event.xdata and event.ydata
  • event.col and event.row
  • event.pos

Answer: event.xdata and event.ydata. event.xdata and event.ydata give the click translated into the plot's data coordinates.

What does event.inaxes let you check?

  • The figure DPI
  • Whether the click landed inside an Axes
  • The mouse button number
  • The backend name

Answer: Whether the click landed inside an Axes. Guarding with 'if event.inaxes' avoids None xdata/ydata when a click misses the axes.

Which event reports a pressed key in event.key?

  • type_event
  • char_event
  • key_press_event
  • input_event

Answer: key_press_event. 'key_press_event' delivers the pressed key (like 'up' or 'enter') in event.key.

Which event fires continuously as the mouse moves?

  • motion_notify_event
  • drag_event
  • move_event
  • hover_event

Answer: motion_notify_event. 'motion_notify_event' fires as the cursor moves, ideal for a live readout.

How do you make an artist clickable for pick_event?

  • clickable=True
  • selectable=True
  • picker=True (or a tolerance)
  • pickable=1

Answer: picker=True (or a tolerance). Plot with picker=True (or a pixel tolerance) to enable picking, then connect 'pick_event'.

In a pick callback, which attribute is the clicked artist?

  • event.target
  • event.artist
  • event.object
  • event.line

Answer: event.artist. event.artist is the line or marker that was picked; event.ind lists nearby indices.

Why might clicks do nothing in a headless or inline backend?

  • The figure is too small
  • Events need a live GUI backend and window
  • mpl_connect is deprecated
  • The callback must return True

Answer: Events need a live GUI backend and window. Interactive events require a live GUI backend (TkAgg/Qt); inline/headless backends draw static images.

Which call efficiently schedules a redraw for frequent updates?

  • fig.canvas.draw_idle()
  • fig.refresh()
  • plt.redraw()
  • ax.update()

Answer: fig.canvas.draw_idle(). draw_idle() schedules a redraw efficiently instead of forcing one on every event.