Registers a function to be run when a key is pressed in a Matplotlib visualizer. fn
will run if key
is pressed at any time when the plot window is in focus. To narrow the focus to a particular plot, define catchKeypress()
in a subclass of ChartPlot
.
Parameters
key — str, required
The key to listen for. The string must be one character; other strings, or strings not corresponding to a single key, will be ignored.
fn — func(model, event), required
A function to run when
key
is pressed in the Matplotlib visualization.model
is the model object, andevent
is the Matplotlib event object.
charwick
Feb 03, 2021 at 21:31When modifying the visualization after a keypress, always access the figure through
event.canvas.figure
and not throughmodel.visual.fig
. The former will correctly route the event to the currently open figure, whereas the latter will route the event to the figure corresponding to the currently active model, even if the key was pressed in the window of a terminated model.