Saving figures lets you keep your charts as image files. You can share or use them later without rerunning code.
Saving figures to files in Matplotlib
plt.savefig('filename.png', dpi=300, bbox_inches='tight')
filename.png is the name and format of the saved file.
dpi controls image quality (dots per inch).
bbox_inches='tight' trims extra white space around the figure.
plt.savefig('plot.png')plt.savefig('plot.pdf')plt.savefig('plot.jpg', dpi=150)
plt.savefig('plot.png', bbox_inches='tight')
This code creates a simple line plot and saves it as 'sample_plot.png' with good quality and no extra white space. It then prints a confirmation message.
import matplotlib.pyplot as plt # Create simple data x = [1, 2, 3, 4] y = [10, 20, 25, 30] # Plot the data plt.plot(x, y, marker='o') plt.title('Sample Line Plot') plt.xlabel('X axis') plt.ylabel('Y axis') # Save the figure to a file plt.savefig('sample_plot.png', dpi=200, bbox_inches='tight') print('Figure saved as sample_plot.png')
Always call plt.savefig() before plt.show() because showing the plot can clear it.
You can save in many formats like PNG, JPG, PDF, SVG by changing the file extension.
Use dpi to control image clarity; higher means sharper but bigger files.
Use plt.savefig() to save your plots as image files.
Choose file format by the file extension you provide.
Adjust dpi and bbox_inches for quality and layout.
Practice
plt.savefig() function do in matplotlib?Solution
Step 1: Understand the purpose of
This function is used to save the current plot or figure to a file, such as PNG or PDF.plt.savefig()Step 2: Differentiate from display functions
Functions likeplt.show()display the figure, butplt.savefig()writes it to disk.Final Answer:
It saves the current figure to a file on your computer. -> Option AQuick Check:
plt.savefig()saves figure [OK]
- Confusing plt.savefig() with plt.show()
- Thinking savefig displays the plot
- Assuming savefig clears the figure
Solution
Step 1: Recall the correct function name
The function to save figures isplt.savefig(), notplt.save()or others.Step 2: Check the argument format
The filename must be a string, so it should be in quotes:'plot.png'.Final Answer:
plt.savefig('plot.png') -> Option BQuick Check:
Correct function and string filename [OK]
- Missing quotes around filename
- Using incorrect function name
- Passing filename without quotes
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.savefig('myplot.pdf', dpi=300)
plt.close()Solution
Step 1: Understand plt.savefig() with dpi parameter
Settingdpi=300increases the resolution of the saved image, including PDF files.Step 2: Confirm file saving and closing
The code saves the plot as 'myplot.pdf' with dpi 300, then closes the figure to free memory.Final Answer:
A PDF file named 'myplot.pdf' is saved with high resolution. -> Option CQuick Check:
savefig with dpi=300 saves high-res PDF [OK]
- Thinking dpi is ignored for PDF
- Expecting plot to display automatically
- Confusing plt.close() with plt.show()
import matplotlib.pyplot as plt plt.plot([1, 2, 3], [4, 5, 6]) plt.savefig(myplot.png) plt.show()
Solution
Step 1: Check the filename argument in plt.savefig()
The filename must be a string, so it needs quotes:'myplot.png'.Step 2: Verify order of plt.show() and plt.savefig()
Callingplt.savefig()beforeplt.show()is correct and common practice.Final Answer:
The filename is not enclosed in quotes. -> Option DQuick Check:
Filename must be a string in quotes [OK]
- Omitting quotes around filename
- Thinking plt.show() must come first
- Assuming file extension is wrong
plt.savefig() parameter helps achieve this?Solution
Step 1: Understand the role of bbox_inches
The parameterbbox_inches='tight'trims the saved figure to fit the content closely, removing extra margins.Step 2: Differentiate from other parameters
dpicontrols resolution,transparentcontrols background, andformatsets file type, none affect cropping.Final Answer:
bbox_inches='tight' -> Option AQuick Check:
Use bbox_inches='tight' to crop figure [OK]
- Confusing dpi with cropping
- Using transparent instead of bbox_inches
- Setting format does not crop figure
Ready to prove it?
Pass the test and this pattern moves unknown
