Skip to content

Commit cfe596f

Browse files
committed
Add new example for images and ArtistAnimation
1 parent eaa22e1 commit cfe596f

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

tutorials/introductory/animation_tutorial.py

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,33 @@ def update(frame):
122122
# :class:`~matplotlib.image.AxesImage` object. The data in this object can also
123123
# similarly be modified by using the `.image.AxesImage.set_data` method.
124124

125+
126+
def f(x, y, mean, cov):
127+
dev_x = x - mean
128+
dev_y = y - mean
129+
maha = -0.5 * (((x-mean)/cov)**2 + ((y-mean)/cov)**2)
130+
return (1/(np.pi * cov)) * np.exp(maha)
131+
125132
fig, ax = plt.subplots()
126-
rng = np.random.default_rng()
127133

128-
aximg = ax.imshow(rng.uniform(low=0, high=1, size=(10, 10)), cmap="Blues")
134+
x, y = np.meshgrid(np.arange(-1, 1, 0.01), np.arange(-1, 1, 0.01))
135+
mean = 0
136+
cov = 0.1
137+
data = f(x, y, mean, cov)
138+
aximg = ax.imshow(data)
129139

130140

131141
def update(frame):
132-
data = rng.uniform(low=0, high=1, size=(10, 10))
142+
x, y = np.meshgrid(np.arange(-1, 1, 0.01), np.arange(-1, 1, 0.01))
143+
mean = 0
144+
cov = 0.01 * frame + 1e-6
145+
data = f(x, y, mean, cov)
146+
133147
aximg.set_data(data)
134148
return (aximg,)
135149

136150

137-
ani = animation.FuncAnimation(fig=fig, func=update, frames=None, interval=200)
151+
ani = animation.FuncAnimation(fig=fig, func=update, frames=None, interval=100)
138152
plt.show()
139153

140154
###############################################################################
@@ -146,18 +160,26 @@ def update(frame):
146160
# list of artists is then converted frame by frame into an animation.
147161

148162

163+
def f(x, y, mean, cov):
164+
dev_x = x - mean
165+
dev_y = y - mean
166+
maha = -0.5 * (((x-mean)/cov)**2 + ((y-mean)/cov)**2)
167+
return (1/(np.pi * cov)) * np.exp(maha)
168+
149169
fig, ax = plt.subplots()
150-
ax.grid()
151-
rng = np.random.default_rng()
152170

153-
x_frames = rng.uniform(low=0, high=1, size=(100, 120))
154-
y_frames = rng.uniform(low=0, high=1, size=(100, 120))
171+
x, y = np.meshgrid(np.arange(-1, 1, 0.01), np.arange(-1, 1, 0.01))
172+
mean = 0
173+
cov = 0.1
174+
data = f(x, y, mean, cov)
175+
aximg = ax.imshow(data)
176+
155177
artists = [
156-
[ax.scatter(x_frames[:, i], y_frames[:, i], c="b")]
157-
for i in range(x_frames.shape[-1])
178+
[ax.imshow(f(x, y, mean, 0.01 * frame + 1e-6))]
179+
for frame in range(120)
158180
]
159181

160-
ani = animation.ArtistAnimation(fig=fig, artists=artists, repeat_delay=1000)
182+
ani = animation.ArtistAnimation(fig=fig, artists=artists, interval=100)
161183
plt.show()
162184

163185
###############################################################################

0 commit comments

Comments
 (0)