Skip to content

Commit 88e3bcc

Browse files
committed
Simplify examples by removing data gen functions
1 parent fcc5f30 commit 88e3bcc

File tree

1 file changed

+22
-49
lines changed

1 file changed

+22
-49
lines changed

tutorials/introductory/animation_tutorial.py

Lines changed: 22 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@
2020
# :class:`~matplotlib.animation.FuncAnimation` uses a user-provided function by
2121
# repeatedly calling the function with at a regular *interval*. This allows
2222
# dynamic generation of data by using generators.
23-
import itertools
24-
25-
26-
def data_gen():
27-
for cnt in itertools.count():
28-
t = cnt/10
29-
yield t, np.sin(2 * np.pi * t)
3023

3124

3225
def init():
@@ -43,21 +36,20 @@ def init():
4336
xdata, ydata = [], []
4437

4538

46-
def update(data):
47-
t, y = data
48-
xdata.append(t)
49-
ydata.append(y)
39+
def update(frame):
40+
xdata.append(frame)
41+
ydata.append(np.sin(2 * np.pi * frame / 30))
5042
xmin, xmax = ax.get_xlim()
5143

52-
if t >= xmax:
44+
if frame >= xmax:
5345
ax.set_xlim(xmin, 2*xmax)
5446
ax.figure.canvas.draw()
5547
line.set_data(xdata, ydata)
5648

5749
return line,
5850

5951
ani = animation.FuncAnimation(
60-
fig=fig, func=update, frames=data_gen, init_func=init, interval=20
52+
fig=fig, func=update, frames=240, init_func=init, interval=30
6153
)
6254
plt.show()
6355

@@ -73,25 +65,18 @@ def update(data):
7365
rng.uniform(low=0, high=1, size=100),
7466
c='b'
7567
)
68+
ax.grid()
7669

7770

78-
def data_gen():
79-
for cnt in itertools.count():
80-
x, y = (
81-
rng.uniform(low=0, high=1, size=100),
82-
rng.uniform(low=0, high=1, size=100)
83-
)
84-
yield cnt, x, y
85-
86-
87-
def update(inputs):
88-
frame, x, y = inputs
71+
def update(frame):
72+
x = rng.uniform(low=0, high=1, size=100)
73+
y = rng.uniform(low=0, high=1, size=100)
8974
data = np.stack([x, y]).T
9075
scat.set_offsets(data)
9176
return scat,
9277

9378
ani = animation.FuncAnimation(
94-
fig=fig, func=update, frames=data_gen, interval=200
79+
fig=fig, func=update, frames=240, interval=300
9580
)
9681
plt.show()
9782

@@ -103,22 +88,16 @@ def update(inputs):
10388
fig, ax = plt.subplots()
10489
rng = np.random.default_rng()
10590

106-
aximg = ax.imshow(rng.uniform(low=0, high=1, size=(100, 100)))
107-
91+
aximg = ax.imshow(rng.uniform(low=0, high=1, size=(10, 10)), cmap="Blues")
10892

109-
def data_gen():
110-
for cnt in itertools.count():
111-
x = rng.uniform(low=0, high=1, size=(100, 100))
112-
yield cnt, x
11393

114-
115-
def update(inputs):
116-
frame, x = inputs
117-
aximg.set_data(x)
94+
def update(frame):
95+
data = rng.uniform(low=0, high=1, size=(10, 10))
96+
aximg.set_data(data)
11897
return aximg,
11998

12099
ani = animation.FuncAnimation(
121-
fig=fig, func=update, frames=data_gen, interval=200
100+
fig=fig, func=update, frames=240, interval=200
122101
)
123102
plt.show()
124103

@@ -131,13 +110,14 @@ def update(inputs):
131110

132111

133112
fig, ax = plt.subplots()
113+
ax.grid()
134114
rng = np.random.default_rng()
135115

136116
x_frames = rng.uniform(low=0, high=1, size=(100, 120))
137117
y_frames = rng.uniform(low=0, high=1, size=(100, 120))
138118
artists = [
139119
[
140-
ax.scatter(x_frames[:, i], y_frames[:, i])
120+
ax.scatter(x_frames[:, i], y_frames[:, i], c="b")
141121
]
142122
for i in range(x_frames.shape[-1])
143123
]
@@ -150,6 +130,7 @@ def update(inputs):
150130
# -------------------------------------------
151131

152132
fig, ax = plt.subplots()
133+
ax.grid()
153134
rng = np.random.default_rng()
154135

155136
scat = ax.scatter(
@@ -159,23 +140,15 @@ def update(inputs):
159140
)
160141

161142

162-
def data_gen():
163-
for cnt in itertools.count():
164-
x, y = (
165-
rng.uniform(low=0, high=1, size=100),
166-
rng.uniform(low=0, high=1, size=100)
167-
)
168-
yield cnt, x, y
169-
170-
171-
def update(inputs):
172-
frame, x, y = inputs
143+
def update(frame):
144+
x = rng.uniform(low=0, high=1, size=100)
145+
y = rng.uniform(low=0, high=1, size=100)
173146
data = np.stack([x, y]).T
174147
scat.set_offsets(data)
175148
return scat,
176149

177150
ani = animation.FuncAnimation(
178-
fig=fig, func=update, frames=data_gen, interval=200
151+
fig=fig, func=update, frames=240, interval=200
179152
)
180153
# ani.save(filename="/tmp/pillow_example.gif", writer="pillow")
181154
# ani.save(filename="/tmp/pillow_example.apng", writer="pillow")

0 commit comments

Comments
 (0)