Skip to content

Commit c1b6a1c

Browse files
committed
Simplify the pyplot animation demo.
Focus the pyplot animation demo to just 1) do some simple plotting, 2) call pause(). Yes, it would be more efficient to reuse the same image. But if we are not using the animation module, efficiency is probably the least of our worries.
1 parent 4fc36df commit c1b6a1c

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

examples/animation/animation_demo.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
11
"""
2-
==============
3-
Animation Demo
4-
==============
2+
================
3+
pyplot animation
4+
================
55
6-
Pyplot animation example.
6+
Generating an animation by calling `~.pyplot.pause` between plotting commands.
77
8-
The method shown here is only for very simple, low-performance
9-
use. For more demanding applications, look at the animation
10-
module and the examples that use it.
8+
The method shown here is only suitable for simple, low-performance use. For
9+
more demanding applications, look at the :mod:`animation` module and the
10+
examples that use it.
11+
12+
Note that calling `time.sleep` instead of `~.pyplot.pause` would *not* work.
1113
"""
1214

1315
import matplotlib.pyplot as plt
1416
import numpy as np
1517

16-
x = np.arange(6)
17-
y = np.arange(5)
18-
z = x * y[:, np.newaxis]
18+
np.random.seed(19680801)
19+
data = np.random.random((50, 50, 50))
1920

20-
for i in range(5):
21-
if i == 0:
22-
p = plt.imshow(z)
23-
fig = plt.gcf()
24-
plt.clim() # clamp the color limits
25-
plt.title("Boring slide show")
26-
else:
27-
z = z + 2
28-
p.set_data(z)
21+
fig, ax = plt.subplots()
2922

30-
print("step", i)
31-
plt.pause(0.5)
23+
for i in range(len(data)):
24+
ax.cla()
25+
ax.imshow(data[i])
26+
ax.set_title("frame {}".format(i))
27+
# Note that using time.sleep does *not* work here!
28+
plt.pause(0.1)

0 commit comments

Comments
 (0)