Challenge - 5 Problems
FuncAnimation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple FuncAnimation example
What will be the output of this code snippet that uses FuncAnimation to animate a sine wave?
Matplotlib
import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation fig, ax = plt.subplots() x = np.linspace(0, 2*np.pi, 100) line, = ax.plot(x, np.sin(x)) def update(frame): line.set_ydata(np.sin(x + frame / 10)) return line, ani = FuncAnimation(fig, update, frames=20, blit=True) plt.show()
Attempts:
2 left
💡 Hint
Think about what the update function changes in each frame.
✗ Incorrect
The update function shifts the sine wave horizontally by changing the phase with frame/10. This creates a smooth horizontal animation over 20 frames.
❓ data_output
intermediate1:00remaining
Number of frames executed in FuncAnimation
Given this FuncAnimation setup, how many frames will be executed in total?
Matplotlib
import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation fig = plt.figure() frames = range(5, 15) ani = FuncAnimation(fig, lambda f: f, frames=frames) frame_count = len(list(frames)) print(frame_count)
Attempts:
2 left
💡 Hint
Count how many numbers are in the range from 5 to 15 (exclusive).
✗ Incorrect
The range(5, 15) includes numbers from 5 up to 14, which is 10 numbers total.
🔧 Debug
advanced2:00remaining
Identify the error in this FuncAnimation code
What error will this code raise when run?
Matplotlib
import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation fig, ax = plt.subplots() line, = ax.plot([0, 1], [0, 1]) def update(frame): line.set_ydata([frame, frame + 1]) ani = FuncAnimation(fig, update, frames=range(5), blit=True) plt.show()
Attempts:
2 left
💡 Hint
Check what the update function returns.
✗ Incorrect
The update function does not return anything (returns None). FuncAnimation expects the update function to return an iterable of artists to update.
🧠 Conceptual
advanced1:00remaining
Understanding blit parameter in FuncAnimation
What is the main effect of setting blit=True in FuncAnimation?
Attempts:
2 left
💡 Hint
Think about what 'blitting' means in graphics.
✗ Incorrect
Blitting redraws only the parts of the plot that change, making animations faster and smoother.
🚀 Application
expert3:00remaining
Predict the final y-data after animation
Consider this FuncAnimation code that updates a line's y-data by adding the frame number. What will be the y-data of the line after the last frame (frame=4)?
Matplotlib
import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation fig, ax = plt.subplots() x = np.array([1, 2, 3]) y = np.array([0, 0, 0]) line, = ax.plot(x, y) def update(frame): new_y = y + frame line.set_ydata(new_y) return line, ani = FuncAnimation(fig, update, frames=5, blit=True) # After animation ends, what is line.get_ydata()?
Attempts:
2 left
💡 Hint
The last frame number is 4, and y is added to frame in update.
✗ Incorrect
At the last frame (4), new_y = y + 4 = [0,0,0] + 4 = [4,4,4].