Bird
0
0

What is wrong with this code snippet if the goal is to save a plot as a PDF?

medium📝 Debug Q6 of 15
Matplotlib - Export and Publication Quality
What is wrong with this code snippet if the goal is to save a plot as a PDF?
import matplotlib.pyplot as plt
plt.plot([1, 2, 3])
plt.savefig('plotpdf')  # no extension
AThe file will save as PNG despite missing extension
BSyntax error due to missing extension
CThe file will not save because no extension is given
DThe plot will save as PDF automatically
Step-by-Step Solution
Solution:
  1. Step 1: Check behavior when no extension is provided

    Matplotlib saves the file as PNG by default if no extension is given.
  2. Step 2: Confirm file format saved

    Since 'plotpdf' has no extension, the file will be saved as 'plotpdf' in PNG format.
  3. Final Answer:

    The file saves as PNG despite missing extension -> Option A
  4. Quick Check:

    No extension defaults to PNG format [OK]
Quick Trick: Always specify extension to save in desired format [OK]
Common Mistakes:
  • Expecting error without extension
  • Assuming default PDF format
  • Thinking file won't save

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes