Saving figures lets you keep your charts and graphs as image files. This helps you share or use them later without re-creating them.
Saving figures in Data Analysis Python
plt.savefig('filename.png', dpi=300, bbox_inches='tight')
filename.png is the name and format of the saved file.
dpi controls the image quality (dots per inch).
plt.savefig('chart.png')plt.savefig('plot.pdf', dpi=200)
plt.savefig('graph.jpg', bbox_inches='tight')
This code creates a simple line plot and saves it as 'line_plot.png' with 150 dpi quality. The bbox_inches='tight' trims extra space around the plot.
import matplotlib.pyplot as plt # Create a simple line plot x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 11] plt.plot(x, y) plt.title('Simple Line Plot') # Save the figure as a PNG file plt.savefig('line_plot.png', dpi=150, bbox_inches='tight') print('Figure saved as line_plot.png')
Always call plt.savefig() before plt.show() because showing the plot can clear it.
You can save figures in many formats like PNG, JPG, PDF, SVG by changing the file extension.
Use dpi to control image clarity; higher dpi means better quality but larger file size.
Use plt.savefig() to save your plots as image files.
Choose file format by the file extension you provide.
Set dpi and bbox_inches to control quality and layout.
Practice
Solution
Step 1: Identify the function for saving plots
The functionplt.savefig()is designed to save the current figure to a file.Step 2: Differentiate from other functions
plt.show()displays the plot,plt.plot()creates the plot, andplt.save()does not exist.Final Answer:
plt.savefig() -> Option BQuick Check:
Save plot = plt.savefig() [OK]
- Confusing plt.show() with saving
- Using plt.save() which is not a valid function
- Thinking plt.plot() saves the figure
Solution
Step 1: Check the function name and argument type
The correct function isplt.savefig()and it requires the filename as a string.Step 2: Verify the filename format
The filename must be a string with the extension, e.g., 'plot.png'.plt.savefig('plot.png')matches this exactly.Final Answer:
plt.savefig('plot.png') -> Option AQuick Check:
Save with filename string = plt.savefig('plot.png') [OK]
- Omitting quotes around filename
- Using plt.save() instead of plt.savefig()
- Missing file extension in filename
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.savefig('myplot.pdf', dpi=300, bbox_inches='tight')Solution
Step 1: Understand plt.savefig parameters
The code saves the plot as 'myplot.pdf' with dpi=300 for high resolution and bbox_inches='tight' to reduce whitespace.Step 2: Confirm file format and parameters compatibility
PDF supports dpi and bbox_inches parameters, so no error occurs and the file is saved correctly.Final Answer:
Saves a PDF file named 'myplot.pdf' with high resolution and tight layout -> Option DQuick Check:
Save PDF with dpi and tight bbox = correct [OK]
- Thinking dpi is ignored for PDF
- Confusing save with display (plt.show())
- Assuming dpi causes error for PDF
import matplotlib.pyplot as plt plt.plot([1, 2, 3], [4, 5, 6]) plt.savefig(myplot.png)
Solution
Step 1: Check the argument to plt.savefig()
The filename must be a string, so it needs quotes around 'myplot.png'.Step 2: Verify other parts of the code
Calling plt.plot() before plt.savefig() is correct, dpi is optional, and '.png' is a valid extension.Final Answer:
Missing quotes around filename 'myplot.png' -> Option CQuick Check:
Filename must be string with quotes [OK]
- Forgetting quotes around filename
- Thinking dpi is mandatory
- Misordering plot and savefig calls
Solution
Step 1: Understand dpi and bbox_inches parameters
High dpi (e.g., 600) means better image quality. bbox_inches='tight' reduces extra white space.Step 2: Evaluate each option
plt.savefig('figure.png', dpi=600, bbox_inches='tight')uses dpi=600 and bbox_inches='tight' correctly.plt.savefig('figure.png', dpi=72, bbox_inches='standard')uses low dpi and invalid bbox_inches.plt.savefig('figure.png', dpi=600, bbox_inches='loose')uses bbox_inches='loose' which adds space.plt.savefig('figure.png', dpi='high', bbox_inches='tight')uses invalid dpi value 'high'.Final Answer:
plt.savefig('figure.png', dpi=600, bbox_inches='tight') -> Option AQuick Check:
High dpi + tight bbox = best quality [OK]
- Using invalid dpi values like strings
- Choosing bbox_inches='loose' which adds whitespace
- Ignoring dpi leading to low-quality images
Ready to prove it?
Pass the test and this pattern moves unknown
