0
0
Matplotlibdata~20 mins

Init function for animation in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Animation Init Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of init function in matplotlib animation

Consider the following code snippet using matplotlib.animation.FuncAnimation. What will be the output of the init function?

Matplotlib
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

fig, ax = plt.subplots()
line, = ax.plot([], [], 'r-')

def init():
    line.set_data([], [])
    return line,

def update(frame):
    x = list(range(frame))
    y = [i**2 for i in x]
    line.set_data(x, y)
    return line,

ani = FuncAnimation(fig, update, frames=5, init_func=init, blit=True)
print(init())
ANone
B[]
C(<matplotlib.lines.Line2D object at 0x7f8c8c1d4d30>,)
DSyntaxError
Attempts:
2 left
💡 Hint

Remember that init returns a tuple of artists to be re-drawn.

🧠 Conceptual
intermediate
1:30remaining
Purpose of init function in FuncAnimation

What is the main purpose of the init function when used with matplotlib.animation.FuncAnimation?

ATo set up the background of the animation before frames are drawn
BTo save the animation to a file
CTo update the plot data for each frame
DTo display the plot window
Attempts:
2 left
💡 Hint

Think about what happens before the animation frames start.

🔧 Debug
advanced
2:00remaining
Why does this init function cause an error?

Examine the following init function used in a matplotlib animation. Why does it cause an error?

Matplotlib
def init():
    line.set_data([], [])
AIt does not return the line object as a tuple, causing FuncAnimation to fail
BThe set_data method is called with empty lists, which is invalid
CThe line object is not defined inside the function
DThe function is missing the required frame argument
Attempts:
2 left
💡 Hint

Check what FuncAnimation expects the init function to return.

data_output
advanced
1:30remaining
Data returned by init function in animation

Given this animation setup, what data does the init function return?

Matplotlib
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

fig, ax = plt.subplots()
line, = ax.plot([], [], 'b-')

def init():
    line.set_data([], [])
    return line,

result = init()
AA list containing two empty lists
BA tuple containing a Line2D object with empty data
CNone
DAn empty list
Attempts:
2 left
💡 Hint

Look at what line.set_data and the return statement do.

🚀 Application
expert
2:00remaining
Effect of missing init_func in FuncAnimation

What happens if you omit the init_func parameter when creating a FuncAnimation?

AThe animation will automatically create a default init function
BThe animation will not run and raises an error
CThe animation will run but with a blank background every frame
DThe animation starts with the plot elements in their current state without resetting
Attempts:
2 left
💡 Hint

Consider what init_func does and what happens if it is not provided.