>>> def oneat(x): ... print('eat', x) >>> def ondrink(x): ... print('drink', x)>>> from matplotlib.cbook import CallbackRegistry >>> callbacks = CallbackRegistry()>>> id_eat = callbacks.connect('eat', oneat) >>> id_drink = callbacks.connect('drink', ondrink)>>> callbacks.process('drink', 123) drink 123 >>> callbacks.process('eat', 456) eat 456 >>> callbacks.process('be merry', 456) # nothing will be called>>> callbacks.disconnect(id_eat) >>> callbacks.process('eat', 456) # nothing will be called>>> with callbacks.blocked(signal='drink'): ... callbacks.process('drink', 123) # nothing will be called >>> callbacks.process('drink', 123) drink 123
In practice, one should always disconnect all callbacks when they are no longer needed to avoid dangling references (and thus memory leaks). However, real code in Matplotlib rarely does so, and due to its design, it is rather difficult to place this kind of code. To get around this, and prevent this class of memory leaks, we instead store weak references to bound methods only, so when the destination object needs to die, the CallbackRegistry won't keep it alive.
If not None, exception_handler must be a function that takes an :None:None:`Exception`
as single parameter. It gets called with any :None:None:`Exception`
raised by the callbacks during CallbackRegistry.process
, and may either re-raise the exception or handle it in another manner.
The default handler prints the exception (with traceback.print_exc
) if an interactive event loop is running; it re-raises the exception if no interactive event loop is running.
Handle registering, processing, blocking, and disconnecting for a set of signals and callbacks:
The following pages refer to to this document either explicitly or contain code examples using this.
matplotlib.axes._axes.Axes
matplotlib.figure.Figure
Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.
Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)
SVG is more flexible but power hungry; and does not scale well to 50 + nodes.
All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them