Reference 〉 Function


An event handling method that can be overridden in subclasses to catch pick (pick_event), keypress (key_press_event), and mouse click (button_press_event) events that occur inside a particular plot. This is distinct from functions passed to MPLVisualization.addKeypress(), as those will run for keys pressed any time the visualization window is in focus, regardless of the mouse position.

For example, pressing ‘l’ with the mouse over a AgentsPlot will rotate the layout. The key pressed can be accessed with event.key.

This function should not be called by user code, but it may be defined in subclasses of ChartPlot. event.canvas.draw_idle() should be called at the end if the plot needs to be refreshed following the event. See the Matplotlib events documentation for more on interacting with the event object.


Notes and Examples

  1. charwick

    Jun 14, 2021 at 23:59

    This function can handle multiple types of events, which can be distinguished with This example handles three types of events in a AgentsPlot:

    1. A keypress event (specifically when the key is L) to rotate the layout,
    2. A pick event (when the Artist representing an agent is clicked on) to identify the Agent object of the clicked Artist and pass it to a hook, and
    3. A click event, to identify the Patch object underneath the clicked coordinates and pass it to a hook.
    def MPLEvent(self, event):
    	if'key_press_event' and event.key=='l': self.rotateLayout()
    		pk = list(self.pos.keys())
    		agents = [self.viz.model.agent(pk[i]) for i in event.ind]
    		self.viz.model.doHooks('agentClick', [agents, self, self.viz.scrubval])
    	elif'button_press_event' and self.layout=='patchgrid':
    		self.viz.model.doHooks('patchClick', [self.viz.model.patches[round(event.xdata), round(event.ydata)], self, self.viz.scrubval])
  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.