0
0
Matplotlibdata~30 mins

Stacked area chart in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Stacked Area Chart
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use plt.show() to display the chart window.