Book Image

Interactive Applications using Matplotlib

Book Image

Interactive Applications using Matplotlib

Overview of this book

Table of Contents (12 chapters)

Making the connection


The callback system is figure-oriented. Any GUI action that can trigger a callback can only happen to whichever figure window is currently in focus. There are no global actions that can trigger callbacks across multiple figures. The callback function will get an Event object that contains pertinent information about the fired event. In the following example, we will hook up two events to a figure: a keyboard button press and a mouse button press. There are two callback functions, each printing out some of the available pieces of information for their respective events:

Code: chp2/basic_mpl_connect.py

from __future__ import print_function 
import matplotlib.pyplot as plt 

def process_key(event): 
    print("Key:", event.key) 
def process_button(event): 
    print("Button:", event.x, event.y, event.xdata, event.ydata, event.button) 

fig, ax = plt.subplots(1, 1) 
fig.canvas.mpl_connect('key_press_event', process_key) 
fig.canvas.mpl_connect('button_press_event', process_button...