0
0
Matplotlibdata~5 mins

Saving figures to files in Matplotlib - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What function in matplotlib do you use to save a figure to a file?
You use plt.savefig() to save the current figure to a file.
Click to reveal answer
beginner
How do you save a figure as a PNG file named 'plot.png'?
Use plt.savefig('plot.png') to save the figure as a PNG file named 'plot.png'.
Click to reveal answer
intermediate
What parameter do you use in savefig() to set the resolution of the saved image?
Use the dpi parameter to set the resolution, for example plt.savefig('plot.png', dpi=300).
Click to reveal answer
beginner
True or False: You must call plt.show() before plt.savefig() to save the figure.
False. You should call plt.savefig() before plt.show() because plt.show() can clear the figure.
Click to reveal answer
intermediate
How can you save a figure in vector format using matplotlib?
Save the figure as a PDF or SVG by using the file extension in savefig(), for example plt.savefig('plot.pdf') or plt.savefig('plot.svg').
Click to reveal answer
Which command saves the current matplotlib figure as a JPEG file named 'image.jpg'?
Aplt.save('image.jpg')
Bplt.savefig('image.jpg')
Cplt.savefig('image.png')
Dplt.export('image.jpg')
What does the dpi parameter control in plt.savefig()?
AThe resolution (dots per inch) of the saved image
BThe size of the figure in inches
CThe color of the figure background
DThe file format of the saved image
If you want to save a figure as a vector graphic, which file extension should you use?
A.png
B.jpg
C.bmp
D.svg
When should you call plt.savefig() in your code?
ABefore <code>plt.show()</code>
BOnly if you want to display the figure
CIt does not matter
DAfter <code>plt.show()</code>
Which of these is NOT a valid file format for plt.savefig()?
A.png
B.pdf
C.exe
D.svg
Explain how to save a matplotlib figure to a file with high resolution.
Think about the function name and the dpi setting.
You got /4 concepts.
    Describe the difference between saving a figure as PNG and SVG in matplotlib.
    Consider image quality and scaling.
    You got /4 concepts.