📖 Scenario: You work at a small local store that tracks daily sales. You want to show the sales data visually to understand trends better.
🎯 Goal: Create a simple bar chart using Matplotlib to display daily sales amounts.
📋 What You'll Learn
Create a dictionary with sales data for 5 days
Set a color variable for the bars
Use Matplotlib to create a bar chart from the sales data
Display the bar chart
💡 Why This Matters
🌍 Real World
Visualizing sales data helps store owners quickly see which days have higher sales and make better business decisions.
💼 Career
Data visualization is a key skill for data analysts and scientists to communicate insights clearly to others.
Progress0 / 4 steps
1
Create sales data dictionary
Create a dictionary called daily_sales with these exact entries: 'Monday': 150, 'Tuesday': 200, 'Wednesday': 170, 'Thursday': 220, 'Friday': 190.
Raspberry Pi
Hint
Use curly braces to create a dictionary with days as keys and sales as values.
2
Set bar color
Create a variable called bar_color and set it to the string 'skyblue' to use as the color for the bars in the chart.
Raspberry Pi
Hint
Assign the string 'skyblue' to the variable bar_color.
3
Create bar chart with Matplotlib
Import matplotlib.pyplot as plt. Use plt.bar() with daily_sales.keys() as the x-axis and daily_sales.values() as the heights. Set the color parameter to bar_color.
Raspberry Pi
Hint
Use plt.bar(x, height, color=bar_color) where x is the days and height is the sales values.
4
Display the bar chart
Use plt.show() to display the bar chart you created.
Raspberry Pi
Hint
Call plt.show() to open a window with the bar chart.
Practice
(1/5)
1. What is the main purpose of using matplotlib in data science on a Raspberry Pi?
easy
A. To create visual pictures like charts from data
B. To store data in a database
C. To write data to a text file
D. To perform complex mathematical calculations
Solution
Step 1: Understand matplotlib's role
Matplotlib is a tool used to turn data into visual charts and graphs.
Step 2: Compare options with matplotlib's function
Only To create visual pictures like charts from data describes creating visual pictures, which matches matplotlib's purpose.
Final Answer:
To create visual pictures like charts from data -> Option A
Quick Check:
Matplotlib = Visual charts [OK]
Hint: Matplotlib = making charts from data [OK]
Common Mistakes:
Confusing matplotlib with data storage tools
Thinking matplotlib does calculations only
Mixing up file writing with visualization
2. Which of the following is the correct way to import matplotlib's plotting module in Python on Raspberry Pi?
easy
A. import matplotlib as plt
B. import matplotlib.plot as plt
C. import matplotlib.plt as pyplot
D. import matplotlib.pyplot as plt
Solution
Step 1: Recall the standard import syntax
The common way to import matplotlib's plotting is import matplotlib.pyplot as plt.
Step 2: Check each option for correctness
Only import matplotlib.pyplot as plt matches the correct module name and alias.
Final Answer:
import matplotlib.pyplot as plt -> Option D
Quick Check:
Correct import = import matplotlib.pyplot as plt [OK]
Hint: Remember: pyplot, not plot or plt alone [OK]
Common Mistakes:
Using wrong module names like 'plot' or 'plt' directly
Mixing up alias names
Typo in 'pyplot'
3. What will the following code display on a Raspberry Pi?
The plt.pie() function accepts sizes as list and labels as list without error.
Step 2: Verify optional parameters
Title and colors are optional; missing them does not cause errors.
Final Answer:
No error, code will run and show pie chart -> Option B
Quick Check:
plt.pie() works with lists and labels [OK]
Hint: Pie chart needs sizes and labels only, colors optional [OK]
Common Mistakes:
Thinking title is mandatory
Believing sizes must be numpy arrays
Assuming colors argument is required
5. You want to plot a bar chart on Raspberry Pi showing sales for products A, B, and C with values 5, 7, and 3. Which code correctly creates this chart with labels and title?
hard
A. import matplotlib.pyplot as plt
products = ['A', 'B', 'C']
sales = [5, 7, 3]
plt.scatter(products, sales)
plt.xlabel('Products')
plt.ylabel('Sales')
plt.title('Sales Bar Chart')
plt.show()
B. import matplotlib.pyplot as plt
products = ['A', 'B', 'C']
sales = [5, 7, 3]
plt.plot(products, sales)
plt.xlabel('Sales')
plt.ylabel('Products')
plt.title('Sales Bar Chart')
plt.show()
C. import matplotlib.pyplot as plt
products = ['A', 'B', 'C']
sales = [5, 7, 3]
plt.bar(products, sales)
plt.xlabel('Products')
plt.ylabel('Sales')
plt.title('Sales Bar Chart')
plt.show()
D. import matplotlib.pyplot as plt
products = ['A', 'B', 'C']
sales = [5, 7, 3]
plt.pie(sales, labels=products)
plt.title('Sales Bar Chart')
plt.show()
Solution
Step 1: Identify correct chart type for sales data
Bar charts show quantities per category, so plt.bar() is correct.
Step 2: Check axis labels and title
Labels for products (x-axis) and sales (y-axis) match the data meaning.
Step 3: Verify other options
import matplotlib.pyplot as plt
products = ['A', 'B', 'C']
sales = [5, 7, 3]
plt.plot(products, sales)
plt.xlabel('Sales')
plt.ylabel('Products')
plt.title('Sales Bar Chart')
plt.show() uses line plot with swapped labels, C uses scatter plot, D uses pie chart which is different visualization.
Final Answer:
Code using plt.bar() with correct labels and title -> Option C
Quick Check:
Bar chart = plt.bar() with correct labels [OK]
Hint: Use plt.bar() for bar charts, label axes clearly [OK]
Common Mistakes:
Using plt.plot() or plt.scatter() for bar charts
Swapping x and y axis labels
Confusing pie chart with bar chart
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.