Bird
0
0

You want to save the same plot in three formats: PNG, SVG, and PDF. Which code snippet correctly saves the plot in all three formats?

hard📝 Application Q15 of 15
Matplotlib - Export and Publication Quality
You want to save the same plot in three formats: PNG, SVG, and PDF. Which code snippet correctly saves the plot in all three formats?
Aplt.plot([1,2,3],[3,2,1]) plt.savefig('plot.png') plt.savefig('plot.jpg') plt.savefig('plot.pdf')
Bplt.plot([1,2,3],[3,2,1]) plt.savefig('plot') plt.savefig('plot') plt.savefig('plot')
Cplt.plot([1,2,3],[3,2,1]) plt.savefig('plot.png', format='png') plt.savefig('plot.svg', format='svg') plt.savefig('plot.pdf', format='pdf')
Dplt.plot([1,2,3],[3,2,1]) plt.save('plot.png') plt.save('plot.svg') plt.save('plot.pdf')
Step-by-Step Solution
Solution:
  1. Step 1: Check function names and parameters

    The correct function is plt.savefig(). plt.plot([1,2,3],[3,2,1]) plt.save('plot.png') plt.save('plot.svg') plt.save('plot.pdf') uses plt.save(), which is invalid.
  2. Step 2: Confirm saving with explicit format or extension

    plt.plot([1,2,3],[3,2,1]) plt.savefig('plot') plt.savefig('plot') plt.savefig('plot') uses filenames without extensions, so format is unclear. plt.plot([1,2,3],[3,2,1]) plt.savefig('plot.png') plt.savefig('plot.svg') plt.savefig('plot.pdf') relies on extensions only, which works but may be less explicit.
  3. Step 3: Understand explicit format argument

    plt.plot([1,2,3],[3,2,1]) plt.savefig('plot.png', format='png') plt.savefig('plot.svg', format='svg') plt.savefig('plot.pdf', format='pdf') uses both filename and explicit format argument, ensuring correct file type saving.
  4. Final Answer:

    Saves the plot in PNG, SVG, and PDF formats using explicit format arguments. -> Option C
  5. Quick Check:

    Use plt.savefig(filename, format='ext') for clarity [OK]
Quick Trick: Use plt.savefig with filename and format='ext' for multiple saves [OK]
Common Mistakes:
  • Using plt.save() instead of plt.savefig()
  • Saving without file extensions causing format errors
  • Not specifying format when filename lacks extension

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes