Bird
0
0

Identify the error in this code that attempts to save a plot as SVG:

medium📝 Debug Q7 of 15
Matplotlib - Export and Publication Quality
Identify the error in this code that attempts to save a plot as SVG:
import matplotlib.pyplot as plt
plt.plot([1, 2, 3])
plt.savefig('graph.svg', format='png')
AMissing plt.show() before savefig
BConflict between filename extension and format argument
CSyntax error in savefig call
DNo error, code runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Analyze filename and format argument

    The filename ends with '.svg' but format='png' is specified, causing a conflict.
  2. Step 2: Understand matplotlib behavior

    Matplotlib prioritizes the format argument, so it saves as PNG but filename extension is SVG, causing confusion.
  3. Final Answer:

    Conflict between filename extension and format argument -> Option B
  4. Quick Check:

    Filename extension and format argument must match [OK]
Quick Trick: Match file extension and format argument to avoid confusion [OK]
Common Mistakes:
  • Ignoring mismatch between extension and format
  • Thinking plt.show() is required before savefig
  • Assuming no error occurs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes