0
0
Matplotlibdata~20 mins

Why animations show change over time in Matplotlib - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Animation Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this animation frame update?
Consider this snippet from a matplotlib animation function that updates a line's y-data over frames. What will be the y-data of the line at frame 3?
Matplotlib
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)

fig, ax = plt.subplots()
line, = ax.plot(x, y)

def update(frame):
    line.set_ydata(np.sin(x + frame / 10))
    return line,

ani = FuncAnimation(fig, update, frames=5, blit=True)

# What is line.get_ydata() at frame=3?
A[sin(x + 0.3)] array values
B[sin(x + 3)] array values
C[sin(x + 30)] array values
D[sin(x + 0.03)] array values
Attempts:
2 left
💡 Hint
Look at how frame is divided by 10 in the update function.
data_output
intermediate
1:00remaining
How many frames will this animation run?
Given this FuncAnimation call, how many frames will the animation produce?
Matplotlib
from matplotlib.animation import FuncAnimation

ani = FuncAnimation(fig, update, frames=range(10), blit=True)
A9
B1
C11
D10
Attempts:
2 left
💡 Hint
Check the length of range(10).
visualization
advanced
2:30remaining
What does this animation visualize over time?
This animation updates a scatter plot's points moving along a circle. What is the shape traced by the points over time?
Matplotlib
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

fig, ax = plt.subplots()
points, = ax.plot([], [], 'bo')

ax.set_xlim(-1.5, 1.5)
ax.set_ylim(-1.5, 1.5)

angles = np.linspace(0, 2*np.pi, 20)

def update(frame):
    x = np.cos(angles + frame * 0.1)
    y = np.sin(angles + frame * 0.1)
    points.set_data(x, y)
    return points,

ani = FuncAnimation(fig, update, frames=100, blit=True)
APoints moving along a straight line
BPoints moving around a circle
CPoints moving randomly
DPoints moving in a spiral outward
Attempts:
2 left
💡 Hint
Look at how x and y are calculated using cosine and sine.
🧠 Conceptual
advanced
1:30remaining
Why does matplotlib animation show change over time?
What is the main reason matplotlib animations show changes over time?
ABecause the update function changes the plot elements for each frame
BBecause matplotlib redraws the entire figure every second automatically
CBecause the data is static but the window refreshes randomly
DBecause the animation saves multiple images without displaying them
Attempts:
2 left
💡 Hint
Think about what the update function does in FuncAnimation.
🔧 Debug
expert
3:00remaining
What error does this animation code raise?
This code tries to animate a line but raises an error. What error is it?
Matplotlib
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

x = np.linspace(0, 2*np.pi, 100)
fig, ax = plt.subplots()
line, = ax.plot(x, np.sin(x))

def update(frame):
    y = np.sin(x + frame / 10)
    line.set_ydata(y)
    return line,

ani = FuncAnimation(fig, update, frames=10, blit=True)
plt.show()
ATypeError: 'NoneType' object is not iterable
BValueError: x and y must have same first dimension
CRuntimeError: Animation function must return an iterable of artists
DNo error, runs fine
Attempts:
2 left
💡 Hint
Check what the update function returns.