Bird
0
0

What will happen when this code is executed?

medium📝 Predict Output Q5 of 15
Matplotlib - Interactive Features
What will happen when this code is executed?
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('TkAgg')
plt.plot([1, 2, 3], [4, 5, 6])
plt.show()
AThe plot is saved to a file named 'plot.png'
BThe plot displays correctly in a window
CAn error occurs because backend must be set before importing pyplot
DNothing happens; the script runs silently
Step-by-Step Solution
Solution:
  1. Step 1: Import pyplot before setting backend

    Backend must be set before importing pyplot to take effect.
  2. Step 2: Setting backend after pyplot import

    This causes a runtime error because backend is already initialized.
  3. Step 3: plt.show() call

    This will not execute properly due to the prior error.
  4. Final Answer:

    An error occurs due to backend set after pyplot import -> Option C
  5. Quick Check:

    Set backend before importing pyplot [OK]
Quick Trick: Always set backend before importing pyplot [OK]
Common Mistakes:
  • Setting backend after importing pyplot
  • Expecting plot window to open despite error
  • Confusing plt.show() behavior with backend issues

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes