Bird
0
0

Given this code snippet, what will be the output when the animation starts?

medium📝 Predict Output Q13 of 15
Matplotlib - Animations
Given this code snippet, what will be the output when the animation starts?
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

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

xdata, ydata = [], []

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

def update(frame):
    xdata.append(frame)
    ydata.append(frame ** 2)
    line.set_data(xdata, ydata)
    return line,

ani = FuncAnimation(fig, update, frames=range(3), init_func=init, blit=True)
plt.show()
AAn empty plot appears first, then points (0,0), (1,1), (2,4) are drawn
BThe plot shows points (0,0), (1,1), (2,4) immediately without empty start
CThe plot remains empty throughout the animation
DThe code raises an error because init returns a tuple
Step-by-Step Solution
Solution:
  1. Step 1: Analyze init function effect

    The init function clears the line data to empty lists, so the plot starts empty.
  2. Step 2: Analyze update function over frames

    For frames 0,1,2, points (0,0), (1,1), (2,4) are appended and drawn sequentially.
  3. Final Answer:

    An empty plot appears first, then points (0,0), (1,1), (2,4) are drawn -> Option A
  4. Quick Check:

    Init clears plot; update adds points [OK]
Quick Trick: Init clears plot; update adds points frame-wise [OK]
Common Mistakes:
  • Assuming plot shows points immediately without empty start
  • Thinking init returning tuple causes error
  • Confusing update and init roles

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes