Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pyplot: add pause() function for crude animation #148

Merged
merged 1 commit into from Jun 20, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions examples/pylab_examples/animation_demo.py
@@ -0,0 +1,30 @@
"""
Pyplot animation example.

The method shown here is only for very simple, low-performance
use. For more demanding applications, look at the animation
module and the examples that use it.
"""

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(6)
y = np.arange(5)
z = x * y[:,np.newaxis]

for i in xrange(5):
if i==0:
p = plt.imshow(z)
fig = plt.gcf()
plt.clim() # clamp the color limits
plt.title("Boring slide show")
else:
z = z + 2
p.set_data(z)

print "step", i
plt.pause(0.5)



1 change: 1 addition & 0 deletions lib/matplotlib/pylab.py
Expand Up @@ -61,6 +61,7 @@
loglog - a log log plot
matshow - display a matrix in a new figure preserving aspect
margins - set margins used in autoscaling
pause - pause for a specified interval
pcolor - make a pseudocolor plot
pcolormesh - make a pseudocolor plot using a quadrilateral mesh
pie - make a pie chart
Expand Down
37 changes: 36 additions & 1 deletion lib/matplotlib/pyplot.py
Expand Up @@ -146,6 +146,41 @@ def ion():
'Turn interactive mode on.'
matplotlib.interactive(True)

def pause(interval):
"""
Pause for *interval* seconds.

If there is an active figure it will be updated and displayed,
and the gui event loop will run during the pause.

If there is no active figure, or if a non-interactive backend
is in use, this executes time.sleep(interval).

This can be used for crude animation. For more complex
animation, see :mod:`matplotlib.animation`.

"""
backend = rcParams['backend']
if backend in _interactive_bk:
figManager = _pylab_helpers.Gcf.get_active()
if figManager is not None:
canvas = figManager.canvas
canvas.draw()
was_interactive = isinteractive()
if not was_interactive:
ion()
show()
canvas.start_event_loop(interval)
if not was_interactive:
ioff()
return

# No on-screen figure is active, so sleep() is all we need.
import time
time.sleep(interval)



@docstring.copy_dedent(matplotlib.rc)
def rc(*args, **kwargs):
matplotlib.rc(*args, **kwargs)
Expand Down Expand Up @@ -958,7 +993,7 @@ def subplot_tool(targetfig=None):

def tight_layout(pad=1.2, h_pad=None, w_pad=None):
"""Adjust subplot parameters to give specified padding.

Parameters
----------
pad : float
Expand Down