📖 Scenario: You work as a data analyst for a small company. You have sales data for three products over four quarters. Your manager wants to see how the total sales and individual product sales changed over time in a clear visual format.
🎯 Goal: Create a stacked area chart using matplotlib to show the sales trends of three products over four quarters.
📋 What You'll Learn
Create a dictionary called sales_data with keys 'Q1', 'Q2', 'Q3', 'Q4' and values as lists of sales numbers for three products.
Create a list called quarters with the values 'Q1', 'Q2', 'Q3', 'Q4'.
Use matplotlib.pyplot to create a stacked area chart.
Label the x-axis as 'Quarter' and y-axis as 'Sales'.
Add a legend with product names 'Product A', 'Product B', and 'Product C'.
💡 Why This Matters
🌍 Real World
Stacked area charts are useful to visualize how different parts contribute to a whole over time, such as sales of multiple products across quarters.
💼 Career
Data analysts and scientists often create stacked area charts to present trends and comparisons clearly to stakeholders.
Progress0 / 4 steps
1
Create the sales data dictionary
Create a dictionary called sales_data with keys 'Q1', 'Q2', 'Q3', 'Q4'. Each key should have a list of three sales numbers: [100, 150, 200] for 'Q1', [120, 160, 210] for 'Q2', [130, 170, 220] for 'Q3', and [140, 180, 230] for 'Q4'.
Matplotlib
Hint
Use curly braces {} to create the dictionary. Each key is a quarter string, and the value is a list of three numbers.
2
Create the quarters list
Create a list called quarters with the values 'Q1', 'Q2', 'Q3', and 'Q4' in this order.
Matplotlib
Hint
Use square brackets [] to create the list with the quarter strings in order.
3
Prepare data and plot the stacked area chart
Import matplotlib.pyplot as plt. Extract the sales lists for each quarter from sales_data in the order of quarters. Create three lists: product_a, product_b, and product_c containing sales for each product across quarters. Use plt.stackplot with quarters and these three lists to create a stacked area chart. Add a legend with labels 'Product A', 'Product B', and 'Product C'. Label the x-axis as 'Quarter' and y-axis as 'Sales'.
Matplotlib
Hint
Use list comprehension to get sales for each product. Use plt.stackplot with the quarters and product lists. Add labels and legend for clarity.
4
Display the stacked area chart
Use plt.show() to display the stacked area chart.
Matplotlib
Hint
Use plt.show() to display the chart window.
Practice
(1/5)
1. What does a stacked area chart in matplotlib primarily show?
easy
A. How different parts add up to a whole over categories or time
B. Only the total sum of all data points
C. The relationship between two unrelated variables
D. The frequency distribution of a single variable
Solution
Step 1: Understand the purpose of stacked area charts
Stacked area charts display multiple data series stacked on top of each other to show how each part contributes to the total over time or categories.
Step 2: Compare options with this purpose
How different parts add up to a whole over categories or time correctly describes this purpose. Options A, B, and C describe other chart types or incorrect uses.
Final Answer:
How different parts add up to a whole over categories or time -> Option A
Quick Check:
Stacked area chart = parts adding to whole [OK]
Hint: Stacked area charts show parts stacked to form a whole [OK]
Common Mistakes:
Confusing stacked area chart with line chart
Thinking it shows only totals, not parts
Mixing it up with scatter plots
2. Which of the following is the correct way to create a stacked area chart using matplotlib.pyplot?
easy
A. plt.stackplot(x, y1, y2, labels=['A', 'B'])
B. plt.area(x, y1, y2)
C. plt.plot_area(x, y1, y2)
D. plt.stack(x, y1, y2)
Solution
Step 1: Recall the correct function name
The correct function to create stacked area charts in matplotlib is stackplot from matplotlib.pyplot.
Step 2: Check the syntax
plt.stackplot(x, y1, y2, labels=['A', 'B']) uses plt.stackplot(x, y1, y2, labels=['A', 'B']), which is correct. Other options use non-existent functions.
Final Answer:
plt.stackplot(x, y1, y2, labels=['A', 'B']) -> Option A
Quick Check:
Function name is stackplot [OK]
Hint: Use plt.stackplot for stacked area charts [OK]