A. plt.savefig() requires an additional argument for format.
B. The filename extension '.png' is incorrect for saving images.
C. plt.plot() is missing required arguments.
D. The plot is saved after plt.show(), which may save a blank image.
Solution
Step 1: Understand the order of plt.show() and plt.savefig()
Calling plt.show() displays and clears the figure by default.
Step 2: Identify consequence of saving after show()
Saving after plt.show() often results in an empty or blank image file.
Final Answer:
The plot is saved after plt.show(), which may save a blank image. -> Option D
Quick Check:
Save before show to avoid blank files [OK]
Hint: Always savefig before show() to keep the plot [OK]
Common Mistakes:
Saving after plt.show() causing empty files
Thinking filename extension needs special format argument
Assuming plt.plot() needs more arguments
5. 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
A. plt.plot([1,2,3],[3,2,1])
plt.savefig('plot.png')
plt.savefig('plot.jpg')
plt.savefig('plot.pdf')
B. plt.plot([1,2,3],[3,2,1])
plt.savefig('plot')
plt.savefig('plot')
plt.savefig('plot')
C. 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')
D. plt.plot([1,2,3],[3,2,1])
plt.save('plot.png')
plt.save('plot.svg')
plt.save('plot.pdf')
Solution
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.
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.
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.
Final Answer:
Saves the plot in PNG, SVG, and PDF formats using explicit format arguments. -> Option C
Quick Check:
Use plt.savefig(filename, format='ext') for clarity [OK]
Hint: 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