Bird
0
0

You want to run a Matplotlib script on a server without a display and save plots as PNG files. Which backend and code order is correct?

hard📝 Application Q9 of 15
Matplotlib - Interactive Features
You want to run a Matplotlib script on a server without a display and save plots as PNG files. Which backend and code order is correct?
Aimport matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt plt.plot(...) plt.savefig(...)
Bimport matplotlib.pyplot as plt matplotlib.use('TkAgg') plt.plot(...) plt.savefig(...)
Cimport matplotlib matplotlib.use('Qt5Agg') import matplotlib.pyplot as plt plt.plot(...) plt.savefig(...)
Dimport matplotlib.pyplot as plt plt.plot(...) plt.savefig(...)
Step-by-Step Solution
Solution:
  1. Step 1: Choose backend for no-display server

    'Agg' backend works without GUI and saves plots to files.
  2. Step 2: Set backend before importing pyplot

    Backend must be set before importing pyplot to avoid errors.
  3. Final Answer:

    import matplotlib\nmatplotlib.use('Agg')\nimport matplotlib.pyplot as plt\nplt.plot(...)\nplt.savefig(...) -> Option A
  4. Quick Check:

    No-display server backend and order = B [OK]
Quick Trick: Set 'Agg' backend before pyplot import on headless servers [OK]
Common Mistakes:
  • Using GUI backends on servers
  • Setting backend after pyplot import
  • Not setting backend at all

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes