Bird
0
0

What will the following code output?

medium📝 Predict Output Q4 of 15
Matplotlib - Animations
What will the following code output?
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
background = fig.canvas.copy_from_bbox(ax.bbox)
ax.plot([1, 2], [3, 4])
fig.canvas.restore_region(background)
print(ax.lines)
A[<matplotlib.lines.Line2D object at ...>]
B[]
CNone
DError: AttributeError
Step-by-Step Solution
Solution:
  1. Step 1: Understand plot and background saving

    The line is plotted after saving the background, so it is added to ax.lines.
  2. Step 2: Effect of restore_region on lines

    Restoring background does not remove lines; it restores the canvas background only.
  3. Final Answer:

    List with one Line2D object representing the plotted line -> Option A
  4. Quick Check:

    ax.lines contains plotted lines after restore_region [OK]
Quick Trick: restore_region resets background, not plot elements [OK]
Common Mistakes:
  • Assuming restore_region clears plotted lines
  • Expecting empty list after restore_region
  • Confusing restore_region with ax.clear()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes