8
8
==================================================
9
9
10
10
Matplotlib supports rich interactive figures by embedding figures into
11
- a GUI window. The basic interactions of panning and zooming in as
12
- Axis to inspect your data is 'baked in' to Matplotlib. This is
11
+ a GUI window. The basic interactions of panning and zooming in an
12
+ Axes to inspect your data is 'baked in' to Matplotlib. This is
13
13
supported by a full mouse and keyboard event handling system that
14
14
you can use to build sophisticated interactive graphs.
15
15
@@ -50,7 +50,7 @@ than directly implement the I/O loop [#f2]_. For example "when the
50
50
user clicks on this button, please run this function" or "when the
51
51
user hits the 'z' key, please run this other function". This allows
52
52
users to write reactive, event-driven, programs without having to
53
- delve into the nity -gritty [#f3 ]_ details of I/O. The core event loop
53
+ delve into the nitty -gritty [#f3 ]_ details of I/O. The core event loop
54
54
is sometimes referred to as "the main loop" and is typically started,
55
55
depending on the library, by methods with names like ``_exec ``,
56
56
``run ``, or ``start ``.
@@ -75,13 +75,13 @@ interfaces.
75
75
Command Prompt Integration
76
76
==========================
77
77
78
- So far, so good. We have the REPL (like the IPthon terminal) that
78
+ So far, so good. We have the REPL (like the IPython terminal) that
79
79
lets us interactively send things code to the interpreter and get
80
80
results back. We also have the GUI toolkit that run an event loop
81
81
waiting for user input and let up register functions to be run when
82
82
that happens. However, if we want to do both we have a problem: the
83
83
prompt and the GUI event loop are both infinite loops that each think
84
- *they * are in charge! In order for both the prompt and the GUI widows
84
+ *they * are in charge! In order for both the prompt and the GUI windows
85
85
to be responsive we need a method to allow the loops to 'timeshare' :
86
86
87
87
1. let the GUI main loop block the python process when you want
@@ -110,17 +110,21 @@ Blocking the Prompt
110
110
The simplest "integration" is to start the GUI event loop in
111
111
'blocking' mode and take over the CLI. While the GUI event loop is
112
112
running you can not enter new commands into the prompt (your terminal
113
- may echo the charters typed into standard in , but they will not be
113
+ may echo the characters typed into the terminal , but they will not be
114
114
sent to the Python interpreter because it is busy running the GUI
115
115
event loop), but the figure windows will be responsive. Once the
116
116
event loop is stopped (leaving any still open figure windows
117
117
non-responsive) you will be able to use the prompt again. Re-starting
118
118
the event loop will make any open figure responsive again (and will
119
- process and queued up user interaction).
119
+ process any queued up user interaction).
120
120
121
121
To start the event loop until all open figures are closed use
122
- `.pyplot.show ` as ``pyplot.show(block=True) ``. To start the event
123
- loop for a fixed amount of time (in seconds) use `.pyplot.pause `.
122
+ `.pyplot.show ` as ::
123
+
124
+ pyplot.show(block=True)
125
+
126
+ To start the event loop for a fixed amount of time (in seconds) use
127
+ `.pyplot.pause `.
124
128
125
129
If you are not using `.pyplot ` you can start and stop the event loops
126
130
via `.FigureCanvasBase.start_event_loop ` and
@@ -130,25 +134,9 @@ large GUI application and the GUI event loop should already be running
130
134
for the application.
131
135
132
136
Away from the prompt, this technique can be very useful if you want to
133
- write a script that pauses for user interaction, see
134
- :ref: `interactive_scripts `.
135
-
136
- .. _spin_event_loop :
137
-
138
- Explicitly spinning the Event Loop
139
- ----------------------------------
140
-
141
- .. autosummary ::
142
- :template: autosummary.rst
143
- :nosignatures:
144
-
145
- backend_bases.FigureCanvasBase.flush_events
146
- backend_bases.FigureCanvasBase.draw_idle
147
-
148
-
149
-
150
- This is particularly useful if you want to provide updates to a plot
151
- during a long computation.
137
+ write a script that pauses for user interaction, or displays a figure
138
+ between polling for additional data. See :ref: `interactive_scripts `
139
+ for more details.
152
140
153
141
154
142
Input Hook integration
@@ -197,8 +185,8 @@ more details.
197
185
198
186
.. _interactive_scripts :
199
187
200
- Scripts
201
- =======
188
+ Scripts and functions
189
+ =====================
202
190
203
191
204
192
.. autosummary ::
@@ -211,21 +199,58 @@ Scripts
211
199
figure.Figure.ginput
212
200
pyplot.ginput
213
201
202
+ pyplot.show
203
+ pyplot.pause
204
+
214
205
There are several use-cases for using interactive figures in scripts:
215
206
216
- - progress updates as a long running script progresses
217
207
- capture user input to steer the script
208
+ - progress updates as a long running script progresses
218
209
- streaming updates from a data source
219
210
211
+ Blocking functions
212
+ ------------------
220
213
221
- In the first if you only need to collect points in an Axes you can use
214
+ If you only need to collect points in an Axes you can use
222
215
`.figure.Figure.ginput ` or more generally the tools from
223
216
`.blocking_input ` the tools will take care of starting and stopping
224
217
the event loop for you. However if you have written some custom event
225
218
handling or are using `.widgets ` you will need to manually run the GUI
226
219
event loop using the methods described :ref: `above <cp_block_the_prompt >`.
227
220
228
- In the second caes, if you have open windows that have pending UI
221
+ You can also use the methods described in :ref: `cp_block_the_prompt `
222
+ to suspend run the GUI event loop. Once the loop exits your code will
223
+ resume. In general, anyplace you would use `time.sleep ` you can use
224
+ `.pyplot.pause ` instead with the added benefit of interactive figures.
225
+
226
+ For example, if you want to poll for data you could use something like ::
227
+
228
+ fig, ax = plt.subplots()
229
+ ln, = ax.plot([], [])
230
+
231
+ while True:
232
+ x, y = get_new_data()
233
+ ln.set_data(x, y)
234
+ fig.canvas.draw_idle()
235
+ plt.pause(1)
236
+
237
+ which would poll for new data and update the figure at 1Hz.
238
+
239
+ .. _spin_event_loop :
240
+
241
+ Explicitly spinning the Event Loop
242
+ ----------------------------------
243
+
244
+ .. autosummary ::
245
+ :template: autosummary.rst
246
+ :nosignatures:
247
+
248
+ backend_bases.FigureCanvasBase.flush_events
249
+ backend_bases.FigureCanvasBase.draw_idle
250
+
251
+
252
+
253
+ If you have open windows that have pending UI
229
254
events (mouse clicks, button presses, or draws) you can explicitly
230
255
process those events by calling `.FigureCanvasBase.flush_events `.
231
256
This will run the GUI event loop until all UI events currently waiting
@@ -243,7 +268,7 @@ For example ::
243
268
fig, ax = plt.subplots()
244
269
fig.canvas.show()
245
270
th = np.linspace(0, 2*np.pi, 512)
246
- ax.set_Lima (-1.5, 1.5)
271
+ ax.set_ylim (-1.5, 1.5)
247
272
248
273
ln, = ax.plot(th, np.sin(th))
249
274
@@ -281,9 +306,6 @@ The more frequently you call `.FigureCanvasBase.flush_events` the more
281
306
responsive your figure will feel but at the cost of spending more
282
307
resource on the visualization and less on your computation.
283
308
284
- The third case you will have to integrate updating the ``Aritist ``
285
- instances, calling ``draw_idle ``, and flushing the GUI event loop with your
286
- data I/O.
287
309
288
310
.. _stale_artists :
289
311
@@ -312,7 +334,7 @@ the artists parent. If you wish to suppress a given artist from propagating
312
334
set this attribute to None.
313
335
314
336
`.figure.Figure ` instances do not have a containing artist and their
315
- default callback is `None `. If you call `` .pyplot.ion` and are not in
337
+ default callback is `None `. If you call `.pyplot.ion ` and are not in
316
338
``IPython `` we will install callback to invoke
317
339
`~.backend_bases.FigureCanvasBase.draw_idle ` when ever the
318
340
`.figure.Figure ` becomes stale. In ``IPython `` we use the
@@ -405,7 +427,7 @@ IPython / prompt toolkit
405
427
406
428
With IPython >= 5.0 IPython has changed from using cpython's readline
407
429
based prompt to a ``prompt_toolkit `` based prompt. ``prompt_toolkit ``
408
- has the same conceptual input hook, which is feed into pt via the
430
+ has the same conceptual input hook, which is feed into prompt_toolkit via the
409
431
:meth: `IPython.terminal.interactiveshell.TerminalInteractiveShell.inputhook `
410
432
method. The source for the prompt_toolkit input hooks lives at
411
433
:mod: `IPython.terminal.pt_inputhooks `
0 commit comments