88==================================================
99
1010Matplotlib 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
1313supported by a full mouse and keyboard event handling system that
1414you can use to build sophisticated interactive graphs.
1515
@@ -50,7 +50,7 @@ than directly implement the I/O loop [#f2]_. For example "when the
5050user clicks on this button, please run this function" or "when the
5151user hits the 'z' key, please run this other function". This allows
5252users 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
5454is sometimes referred to as "the main loop" and is typically started,
5555depending on the library, by methods with names like ``_exec ``,
5656``run ``, or ``start ``.
@@ -75,13 +75,13 @@ interfaces.
7575Command Prompt Integration
7676==========================
7777
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
7979lets us interactively send things code to the interpreter and get
8080results back. We also have the GUI toolkit that run an event loop
8181waiting for user input and let up register functions to be run when
8282that happens. However, if we want to do both we have a problem: the
8383prompt 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
8585to be responsive we need a method to allow the loops to 'timeshare' :
8686
87871. let the GUI main loop block the python process when you want
@@ -110,17 +110,21 @@ Blocking the Prompt
110110The simplest "integration" is to start the GUI event loop in
111111'blocking' mode and take over the CLI. While the GUI event loop is
112112running 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
114114sent to the Python interpreter because it is busy running the GUI
115115event loop), but the figure windows will be responsive. Once the
116116event loop is stopped (leaving any still open figure windows
117117non-responsive) you will be able to use the prompt again. Re-starting
118118the 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).
120120
121121To 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 `.
124128
125129If you are not using `.pyplot ` you can start and stop the event loops
126130via `.FigureCanvasBase.start_event_loop ` and
@@ -130,25 +134,9 @@ large GUI application and the GUI event loop should already be running
130134for the application.
131135
132136Away 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.
152140
153141
154142Input Hook integration
@@ -197,8 +185,8 @@ more details.
197185
198186.. _interactive_scripts :
199187
200- Scripts
201- =======
188+ Scripts and functions
189+ =====================
202190
203191
204192.. autosummary ::
@@ -211,21 +199,58 @@ Scripts
211199 figure.Figure.ginput
212200 pyplot.ginput
213201
202+ pyplot.show
203+ pyplot.pause
204+
214205There are several use-cases for using interactive figures in scripts:
215206
216- - progress updates as a long running script progresses
217207- capture user input to steer the script
208+ - progress updates as a long running script progresses
218209- streaming updates from a data source
219210
211+ Blocking functions
212+ ------------------
220213
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
222215`.figure.Figure.ginput ` or more generally the tools from
223216`.blocking_input ` the tools will take care of starting and stopping
224217the event loop for you. However if you have written some custom event
225218handling or are using `.widgets ` you will need to manually run the GUI
226219event loop using the methods described :ref: `above <cp_block_the_prompt >`.
227220
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
229254events (mouse clicks, button presses, or draws) you can explicitly
230255process those events by calling `.FigureCanvasBase.flush_events `.
231256This will run the GUI event loop until all UI events currently waiting
@@ -243,7 +268,7 @@ For example ::
243268 fig, ax = plt.subplots()
244269 fig.canvas.show()
245270 th = np.linspace(0, 2*np.pi, 512)
246- ax.set_Lima (-1.5, 1.5)
271+ ax.set_ylim (-1.5, 1.5)
247272
248273 ln, = ax.plot(th, np.sin(th))
249274
@@ -281,9 +306,6 @@ The more frequently you call `.FigureCanvasBase.flush_events` the more
281306responsive your figure will feel but at the cost of spending more
282307resource on the visualization and less on your computation.
283308
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.
287309
288310.. _stale_artists :
289311
@@ -312,7 +334,7 @@ the artists parent. If you wish to suppress a given artist from propagating
312334set this attribute to None.
313335
314336`.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
316338``IPython `` we will install callback to invoke
317339`~.backend_bases.FigureCanvasBase.draw_idle ` when ever the
318340`.figure.Figure ` becomes stale. In ``IPython `` we use the
@@ -405,7 +427,7 @@ IPython / prompt toolkit
405427
406428With IPython >= 5.0 IPython has changed from using cpython's readline
407429based 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
409431:meth: `IPython.terminal.interactiveshell.TerminalInteractiveShell.inputhook `
410432method. The source for the prompt_toolkit input hooks lives at
411433:mod: `IPython.terminal.pt_inputhooks `
0 commit comments