|
| 1 | +.. _plotting-guide-interactive: |
| 2 | + |
| 3 | +******************* |
| 4 | +Interactive Figures |
| 5 | +******************* |
| 6 | + |
| 7 | +One of the most powerful ways to use matplotlib is interactive |
| 8 | +figures. At the most basic matplotlib has the ability to zoom and pan |
| 9 | +a figure to inspect your data, however there is also a full mouse and |
| 10 | +keyboard event handling system to enable building sophisticated interactive |
| 11 | +graphs. |
| 12 | + |
| 13 | +This page is meant to be a rapid introduction to the relevant details of |
| 14 | +integrating the matplotlib with a GUI event loop. |
| 15 | + |
| 16 | + |
| 17 | +The GUI event loop |
| 18 | +------------------ |
| 19 | + |
| 20 | +To handle asynchronous user input every GUI framework has an event |
| 21 | +loop. At the most basic this is a stack that can have events to be |
| 22 | +processed. In order for the GUI to be responsive this loop must be |
| 23 | +run. To manage this in python there are two basic methods: |
| 24 | + |
| 25 | +1. let the GUI main loop block the python process |
| 26 | +2. intermittently run the GUI loop for a period of time |
| 27 | + |
| 28 | +Going with option 1 is going down the route of writing a bespoke GUI |
| 29 | +application. In this case, commonly refereed to as 'embedding', the |
| 30 | +GUI event loop is running the show and you should not use the `pyplot` |
| 31 | +layer. Doing anything short of writing a full GUI application |
| 32 | +requires option 2. |
| 33 | + |
| 34 | +The python capi provides a hook, `PyOS_InputHook`, to register a |
| 35 | +function to be run "The function will be called when Python's |
| 36 | +interpreter prompt is about to become idle and wait for user input |
| 37 | +from the terminal.". As an implementation detail of cpython when |
| 38 | +using readline this times out every 0.1 seconds. Using this hook a |
| 39 | +second event loop can be integrated with the terminal. This is done |
| 40 | +in the cpython source for tk, by some GUI framework code (such as |
| 41 | +pyqt), and by IPython. These function typically either exhaust all |
| 42 | +pending events on the GUI event queue or run the main loop for a short |
| 43 | +fixed amount of time. matplotlib does not currently do any management |
| 44 | +of `PyOS_InputHook` due to the wide range of use-cases, this |
| 45 | +management is left to the code using matplotlib. Due to this, |
| 46 | +interactive figures, even with matplotlib in 'interactive mode' may |
| 47 | +not be reliable in the vanilla python repl, we suggest using IPython |
| 48 | +which ensures that such a function is registered for you GUI backend |
| 49 | +of choice. |
| 50 | + |
| 51 | +A draw back of the above approach is that in is only useful while |
| 52 | +python is otherwise idle and waiting for user input. The exact |
| 53 | +methods required to force the GUI to process it's event loop varies |
| 54 | +between frameworks. To enable writing GUI agnostic code, almost all |
| 55 | +of the GUI-based ``Canvas`` classes provide a `flush_event` method. |
| 56 | +By periodically calling this method the GUI can appear to be |
| 57 | +updated and appear to be responsive. |
| 58 | + |
| 59 | +In both cases, scheduling a re-draw of the figure at some point in the |
| 60 | +future use ``fig.canvas.draw_idle()``. This will defer the actual |
| 61 | +rendering of the figure until the GUI is ready to update it's |
| 62 | +on-screen representation. |
| 63 | + |
| 64 | +Stale Artists |
| 65 | +------------- |
| 66 | + |
| 67 | +Artists (as of 1.5) have a ``stale`` attribute which is `True` if the |
| 68 | +internal state of the artist has changed since the last time it was |
| 69 | +drawn to the screen. The stale state is propagated up to the Artists |
| 70 | +parents in the draw tree. Thus, ``fig.stale`` will report of any |
| 71 | +artist in the figure has been modified and out of sync with what is |
| 72 | +displayed on the screen. This is intended to be used to determine if |
| 73 | +``draw_idle`` should be called to schedule a re-rendering of the |
| 74 | +figure. |
| 75 | + |
| 76 | + |
| 77 | +Interactive Mode |
| 78 | +---------------- |
| 79 | + |
| 80 | + |
| 81 | +Blitting |
| 82 | +-------- |
0 commit comments