📖 Scenario: You have created a simple line plot showing sales data over a week. Now, you want to save this plot as an image file so you can share it or use it in reports.
🎯 Goal: Create a line plot using sales data and save the figure as a PNG file named sales_plot.png.
📋 What You'll Learn
Create a list called sales with the values: 150, 200, 250, 300, 280, 320, 400
Create a list called days with the values: 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'
Use matplotlib to plot sales against days
Save the figure as sales_plot.png using matplotlib's savefig method
Print the message "Figure saved as sales_plot.png" after saving
💡 Why This Matters
🌍 Real World
Saving figures is important when you want to share your data visualizations with others or include them in reports and presentations.
💼 Career
Data scientists and analysts often save plots to communicate insights clearly and professionally.
Progress0 / 4 steps
1
Create sales and days data lists
Create a list called sales with these exact values: 150, 200, 250, 300, 280, 320, 400. Also create a list called days with these exact values: 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'.
Data Analysis Python
Hint
Use square brackets to create lists. Separate values with commas.
2
Import matplotlib and set up the plot
Import matplotlib.pyplot as plt. Then use plt.plot(days, sales) to create a line plot of sales over days.
Data Analysis Python
Hint
Use import matplotlib.pyplot as plt to import. Then call plt.plot(days, sales).
3
Save the figure as a PNG file
Use plt.savefig('sales_plot.png') to save the current figure as a PNG file named sales_plot.png.
Data Analysis Python
Hint
Use plt.savefig('sales_plot.png') to save the figure.
4
Print confirmation message
Write a print statement to display the message "Figure saved as sales_plot.png" after saving the figure.
Data Analysis Python
Hint
Use print("Figure saved as sales_plot.png") to show the message.
Practice
(1/5)
1. What is the main function used in Python's matplotlib to save a plot as an image file?
easy
A. plt.show()
B. plt.savefig()
C. plt.plot()
D. plt.save()
Solution
Step 1: Identify the function for saving plots
The function plt.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, and plt.save() does not exist.
Final Answer:
plt.savefig() -> Option B
Quick Check:
Save plot = plt.savefig() [OK]
Hint: Remember: save figure uses 'savefig' word [OK]
Common Mistakes:
Confusing plt.show() with saving
Using plt.save() which is not a valid function
Thinking plt.plot() saves the figure
2. Which of the following is the correct syntax to save a plot as a PNG file with matplotlib?
easy
A. plt.savefig('plot.png')
B. plt.save('plot.png')
C. plt.savefig(plot.png)
D. plt.savefig('plot')
Solution
Step 1: Check the function name and argument type
The correct function is plt.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 A
Quick Check:
Save with filename string = plt.savefig('plot.png') [OK]
Hint: Use quotes around filename with extension [OK]
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 C
Quick Check:
Filename must be string with quotes [OK]
Hint: Always put filename in quotes when saving [OK]
Common Mistakes:
Forgetting quotes around filename
Thinking dpi is mandatory
Misordering plot and savefig calls
5. You want to save a plot as a high-quality PNG file with minimal white space around it. Which of the following code snippets achieves this correctly?