0
0
Matplotlibdata~15 mins

Saving figures to files in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Saving figures to files
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print(f"Figure saved as {filename}") to show the message.