Challenge - 5 Problems
Animation Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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?
Attempts:
2 left
💡 Hint
Look at how frame is divided by 10 in the update function.
✗ Incorrect
The update function shifts the sine wave by frame/10. At frame 3, the shift is 3/10 = 0.3 radians.
❓ data_output
intermediate1: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)
Attempts:
2 left
💡 Hint
Check the length of range(10).
✗ Incorrect
range(10) produces 10 values from 0 to 9, so 10 frames.
❓ visualization
advanced2: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)
Attempts:
2 left
💡 Hint
Look at how x and y are calculated using cosine and sine.
✗ Incorrect
The points are placed on a circle of radius 1, rotating by frame*0.1 radians each frame.
🧠 Conceptual
advanced1:30remaining
Why does matplotlib animation show change over time?
What is the main reason matplotlib animations show changes over time?
Attempts:
2 left
💡 Hint
Think about what the update function does in FuncAnimation.
✗ Incorrect
The update function modifies plot data for each frame, causing visible changes.
🔧 Debug
expert3: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()
Attempts:
2 left
💡 Hint
Check what the update function returns.
✗ Incorrect
When blit=True, the update function must return an iterable of artists to redraw.