Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q6 of 15
Matplotlib - Performance and Large Data
Identify the error in this code snippet:
import matplotlib.pyplot as plt
for i in range(5):
    fig = plt.figure()
    plt.plot([1,2,3], [4,5,6])
plt.close(fig)
ANo error, code runs correctly
Bplt.close(fig) is called outside the loop, closing only last figure
Cplt.close() cannot accept figure objects
Dplt.plot() is called without specifying figure
Step-by-Step Solution
Solution:
  1. Step 1: Check where plt.close(fig) is called

    plt.close(fig) is outside the loop, so only last figure is closed.
  2. Step 2: Effect on memory

    First 4 figures remain open, causing unnecessary memory use.
  3. Final Answer:

    plt.close(fig) is called outside the loop, closing only last figure -> Option B
  4. Quick Check:

    Close inside loop to free all figures [OK]
Quick Trick: Close each figure inside loop to avoid memory leaks [OK]
Common Mistakes:
  • Closing only last figure
  • Assuming plt.close() can't take fig
  • Ignoring memory buildup

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes