📖 Scenario: You work as a data analyst for a small online store. You have sales data for different products over a week. You want to understand how sales vary and spot any patterns or trends.
🎯 Goal: Build a simple Python program that creates a dictionary of sales data, sets a threshold for high sales, filters the data, and then plots the sales using a bar chart to visually reveal patterns.
📋 What You'll Learn
Create a dictionary called sales with product names as keys and sales numbers as values
Create a variable called high_sales_threshold set to 50
Use a dictionary comprehension to create a new dictionary high_sales with only products having sales greater than high_sales_threshold
Use matplotlib.pyplot to plot a bar chart of high_sales
Print the high_sales dictionary
💡 Why This Matters
🌍 Real World
Data analysts often use plots to quickly understand sales trends, customer behavior, or product popularity.
💼 Career
Knowing how to filter data and visualize it is a key skill for data science, business analysis, and reporting roles.
Progress0 / 4 steps
1
Create the sales data dictionary
Create a dictionary called sales with these exact entries: 'Apples': 40, 'Bananas': 55, 'Cherries': 30, 'Dates': 70, 'Elderberries': 20
Matplotlib
Hint
Use curly braces {} to create a dictionary with keys and values separated by colons.
2
Set the high sales threshold
Create a variable called high_sales_threshold and set it to 50
Matplotlib
Hint
Assign the number 50 to the variable high_sales_threshold.
3
Filter high sales products
Use a dictionary comprehension to create a new dictionary called high_sales that includes only products from sales with sales greater than high_sales_threshold
Matplotlib
Hint
Use {product: count for product, count in sales.items() if count > high_sales_threshold} to filter the dictionary.
4
Plot the high sales data and print the dictionary
Import matplotlib.pyplot as plt. Use plt.bar() to create a bar chart of high_sales keys and values. Then use plt.show() to display the plot. Finally, print the high_sales dictionary.
Matplotlib
Hint
Use plt.bar() with high_sales.keys() and high_sales.values(). Then call plt.show(). Finally, print high_sales.
Practice
(1/5)
1. Why do statistical plots help us understand data better?
easy
A. They replace numbers with random colors.
B. They remove all errors from the data automatically.
C. They show data visually, making patterns easier to see.
D. They hide unusual points to simplify data.
Solution
Step 1: Understand the purpose of statistical plots
Statistical plots convert raw numbers into visual forms like charts or graphs.
Step 2: Recognize how visuals reveal patterns
Visuals help spot trends, clusters, or outliers that are hard to see in tables.
Final Answer:
They show data visually, making patterns easier to see. -> Option C
Quick Check:
Visualizing data = easier pattern recognition [OK]
Hint: Visuals reveal trends better than raw numbers [OK]
Common Mistakes:
Thinking plots fix data errors automatically
Believing plots hide unusual data points
Confusing colors with data meaning
2. Which of the following is the correct way to create a simple line plot using matplotlib?
easy
A. plt.plot([1, 2, 3], [4, 5, 6])
B. plt.line([1, 2, 3], [4, 5, 6])
C. plt.graph([1, 2, 3], [4, 5, 6])
D. plt.drawline([1, 2, 3], [4, 5, 6])
Solution
Step 1: Recall matplotlib line plot syntax
The function to plot lines is plt.plot() with x and y data lists.
Step 2: Check each option's function name
Only plt.plot() is a valid matplotlib function for line plots.
Final Answer:
plt.plot([1, 2, 3], [4, 5, 6]) -> Option A
Quick Check:
Correct function name = plt.plot() [OK]
Hint: Use plt.plot() for line plots in matplotlib [OK]
B. A histogram showing frequency distribution of numbers
C. A scatter plot of numbers against their indices
D. A line plot connecting the numbers in order
Solution
Step 1: Understand plt.hist() function
plt.hist() creates a histogram showing frequency counts of data bins.
Step 2: Analyze the data input
The list has repeated numbers; histogram will show how often each appears.
Final Answer:
A histogram showing frequency distribution of numbers -> Option B
Quick Check:
plt.hist() = histogram frequency plot [OK]
Hint: plt.hist() creates frequency distribution plots [OK]
Common Mistakes:
Confusing histogram with bar chart
Expecting scatter or line plot from plt.hist()
Not understanding frequency bins
4. The code below is intended to plot a scatter plot but does not show any points. What is the error?
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [4, 5, 6]
plt.scatter(x, y)
plt.show()
medium
A. Nothing is wrong; the plot will show points.
B. plt.scatter() is misspelled; should be plt.scatterplot()
C. plt.show() is missing, so plot won't display.
D. x and y lists must be numpy arrays, not lists.
Solution
Step 1: Check the code syntax and functions
plt.scatter() is the correct function to plot scatter points; x and y are valid lists.
Step 2: Confirm plot display commands
plt.show() is present, so the plot will display points as expected.
Final Answer:
Nothing is wrong; the plot will show points. -> Option A
Quick Check:
Correct function and plt.show() present = plot visible [OK]
Hint: plt.scatter() with plt.show() displays points [OK]
Common Mistakes:
Thinking plt.scatterplot() is correct function
Believing plt.show() is missing
Assuming lists can't be used for plotting
5. You have a dataset with many values and want to quickly see if there are any unusual points (outliers). Which matplotlib plot is best to reveal this pattern clearly?
hard
A. Scatter plot
B. Line plot
C. Pie chart
D. Box plot
Solution
Step 1: Identify plot types and their uses
Box plots summarize data distribution and highlight outliers clearly.
Step 2: Compare with other plot types
Line plots show trends, pie charts show parts of a whole, scatter plots show relationships but less clear for outliers.