Skip to content

Commit 008c824

Browse files
committed
pyplot: add pause() function for crude animation
New users often want to be able to do simple animation using pyplot functions, but run into trouble trying to use time.sleep() or show(). pause() eliminates the need for either for this application. This was prompted by Sourceforge bug ID 3114540. A modified version of the example given there is included in this changeset.
1 parent af3243d commit 008c824

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Pyplot animation example.
3+
4+
The method shown here is only for very simple, low-performance
5+
use. For more demanding applications, look at the animation
6+
module and the examples that use it.
7+
"""
8+
9+
import matplotlib.pyplot as plt
10+
import numpy as np
11+
12+
x = np.arange(6)
13+
y = np.arange(5)
14+
z = x * y[:,np.newaxis]
15+
16+
for i in xrange(5):
17+
if i==0:
18+
p = plt.imshow(z)
19+
fig = plt.gcf()
20+
plt.clim() # clamp the color limits
21+
plt.title("Boring slide show")
22+
else:
23+
z = z + 2
24+
p.set_data(z)
25+
26+
print "step", i
27+
plt.pause(0.5)
28+
29+
30+

lib/matplotlib/pylab.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
loglog - a log log plot
6262
matshow - display a matrix in a new figure preserving aspect
6363
margins - set margins used in autoscaling
64+
pause - pause for a specified interval
6465
pcolor - make a pseudocolor plot
6566
pcolormesh - make a pseudocolor plot using a quadrilateral mesh
6667
pie - make a pie chart

lib/matplotlib/pyplot.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,41 @@ def ion():
146146
'Turn interactive mode on.'
147147
matplotlib.interactive(True)
148148

149+
def pause(interval):
150+
"""
151+
Pause for *interval* seconds.
152+
153+
If there is an active figure it will be updated and displayed,
154+
and the gui event loop will run during the pause.
155+
156+
If there is no active figure, or if a non-interactive backend
157+
is in use, this executes time.sleep(interval).
158+
159+
This can be used for crude animation. For more complex
160+
animation, see :mod:`matplotlib.animation`.
161+
162+
"""
163+
backend = rcParams['backend']
164+
if backend in _interactive_bk:
165+
figManager = _pylab_helpers.Gcf.get_active()
166+
if figManager is not None:
167+
canvas = figManager.canvas
168+
canvas.draw()
169+
was_interactive = isinteractive()
170+
if not was_interactive:
171+
ion()
172+
show()
173+
canvas.start_event_loop(interval)
174+
if not was_interactive:
175+
ioff()
176+
return
177+
178+
# No on-screen figure is active, so sleep() is all we need.
179+
import time
180+
time.sleep(interval)
181+
182+
183+
149184
@docstring.copy_dedent(matplotlib.rc)
150185
def rc(*args, **kwargs):
151186
matplotlib.rc(*args, **kwargs)
@@ -958,7 +993,7 @@ def subplot_tool(targetfig=None):
958993

959994
def tight_layout(pad=1.2, h_pad=None, w_pad=None):
960995
"""Adjust subplot parameters to give specified padding.
961-
996+
962997
Parameters
963998
----------
964999
pad : float

0 commit comments

Comments
 (0)