Reference 〉 Function

MPLVisualization.addKeypress(keystr, fnfunc(model, event))

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.


  • 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, and event is the Matplotlib event object.

Notes and Examples

  1. charwick

    Feb 03, 2021 at 21:31

    When modifying the visualization after a keypress, always access the figure through event.canvas.figure and not through model.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.

    def toggle(model, event):
    	##This code will toggle the legends in the wrong window if 't' is pressed in the window of a terminated model
    	#for axes in model.visual.fig.axes:
    	#	leg = axes.get_legend()
    	#	leg.set_visible(not leg.get_visible())
    	#This code correctly toggles the legends in the foregrounded window
    	for axes in event.canvas.figure.axes:
    		leg = axes.get_legend()
    		leg.set_visible(not leg.get_visible())
    heli.visual.addKeypress('t', toggle)
  2. Contribute a Note

    Your email address will not be published. Required fields are marked *

    You may use limited HTML for formatting. Please embed blocks of code in <pre><code> </code></pre> tags.