📖 Scenario: You are working on a small data science project where you create simple charts to understand sales data. You want to save these charts as image files so you can share them with your team.
🎯 Goal: Create a bar chart from sales data and save the figure as a PNG file on your computer.
📋 What You'll Learn
Create a dictionary with sales data for three products
Set a filename variable for the image file
Use matplotlib to create a bar chart from the sales data
Save the figure to the file using the filename variable
Print a confirmation message with the filename
💡 Why This Matters
🌍 Real World
Saving charts as image files is common when sharing data insights with others or including visuals in reports.
💼 Career
Data scientists and analysts often save figures to files to document their findings and communicate results clearly.
Progress0 / 4 steps
1
Create sales data dictionary
Create a dictionary called sales with these exact entries: 'Apples': 30, 'Bananas': 45, 'Cherries': 25.
Matplotlib
Hint
Use curly braces to create a dictionary with keys and values separated by colons.
2
Set filename for saving figure
Create a variable called filename and set it to the string 'sales_chart.png'.
Matplotlib
Hint
Use a string variable to store the filename for the image.
3
Create bar chart with matplotlib
Import matplotlib.pyplot as plt. Use plt.bar() with sales.keys() and sales.values() to create a bar chart. Then use plt.savefig(filename) to save the figure to the file.
Matplotlib
Hint
Use plt.bar() to create the bar chart and plt.savefig() to save it.
4
Print confirmation message
Write a print statement that outputs: "Figure saved as sales_chart.png" using the filename variable inside an f-string.
Matplotlib
Hint
Use print(f"Figure saved as {filename}") to show the message.
Practice
(1/5)
1. What does the plt.savefig() function do in matplotlib?
easy
A. It saves the current figure to a file on your computer.
B. It displays the figure on the screen.
C. It clears the current figure.
D. It creates a new figure window.
Solution
Step 1: Understand the purpose of plt.savefig()
This function is used to save the current plot or figure to a file, such as PNG or PDF.
Step 2: Differentiate from display functions
Functions like plt.show() display the figure, but plt.savefig() writes it to disk.
Final Answer:
It saves the current figure to a file on your computer. -> Option A
Quick Check:
plt.savefig() saves figure [OK]
Hint: Remember: savefig saves, show displays [OK]
Common Mistakes:
Confusing plt.savefig() with plt.show()
Thinking savefig displays the plot
Assuming savefig clears the figure
2. Which of the following is the correct syntax to save a figure as a PNG file named 'plot.png'?
easy
A. plt.save('plot.png')
B. plt.savefig('plot.png')
C. plt.savefig(plot.png)
D. plt.save_figure('plot.png')
Solution
Step 1: Recall the correct function name
The function to save figures is plt.savefig(), not plt.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 B
Quick Check:
Correct function and string filename [OK]
Hint: Use plt.savefig('filename.ext') with quotes [OK]