|
4 | 4 | Interactive Figures |
5 | 5 | ******************* |
6 | 6 |
|
7 | | -One of the most powerful ways to use matplotlib is interactive |
| 7 | +One of the most powerful uses of matplotlib is interactive |
8 | 8 | figures. At the most basic matplotlib has the ability to zoom and pan |
9 | 9 | a figure to inspect your data, however there is also a full mouse and |
10 | 10 | keyboard event handling system to enable building sophisticated interactive |
@@ -34,30 +34,34 @@ requires option 2. |
34 | 34 | The python capi provides a hook, `PyOS_InputHook`, to register a |
35 | 35 | function to be run "The function will be called when Python's |
36 | 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 |
| 37 | +from the terminal.". This hook can be used to integrate a second |
| 38 | +event loop with the python repl. Such a hooks are usually included |
| 39 | +with the python bindings for GUI toolkits and may be registered on |
| 40 | +import. IPython also includes hooks for all of the GUI frameworks |
| 41 | +supported by matplotlib. The hook functions typically exhaust |
| 42 | +all pending events on the GUI event queue, run the main loop for a |
| 43 | +short fixed amount of time, or run the event loop until a key is |
| 44 | +pressed on stdin. |
| 45 | + |
| 46 | +matplotlib does not currently do any management of `PyOS_InputHook` |
| 47 | +due to the wide range of ways that matplotlib is used. This |
| 48 | +management is left to the code using matplotlib. Interactive figures, |
| 49 | +even with matplotlib in 'interactive mode', may not work in the |
| 50 | +vanilla python repl if an appropriate `PyOS_InputHook` is not |
| 51 | +registered. We suggest using ``IPython``, which in addition to |
| 52 | +improving the command line, ensures that such a `PyOS_InptuHook` |
| 53 | +function is registered for you GUI backend of choice. |
| 54 | + |
| 55 | +A drawback of relying on `PyOS_InputHook` is that the GUI event loop |
| 56 | +is only processing events while python is otherwise idle and waiting |
| 57 | +for user input. If you want the GUI to be responsive during long |
| 58 | +running code it is necessary to periodically flush the GUI event |
| 59 | +queue. To achive this, almost all of the of the GUI-based ``Canvas`` |
| 60 | +classes provide a `flush_event` method. By periodically calling this |
| 61 | +method the GUI will be updated and appear to be responsive. |
| 62 | + |
| 63 | +In both cases, to schedule a re-draw of the figure at some point in |
| 64 | +the future use ``fig.canvas.draw_idle()``. This will defer the actual |
61 | 65 | rendering of the figure until the GUI is ready to update it's |
62 | 66 | on-screen representation. |
63 | 67 |
|
|
0 commit comments