0
0
Pandasdata~30 mins

Subplots for multiple charts in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Subplots for multiple charts
📖 Scenario: You work as a data analyst for a small store. You have sales data for three products over four months. You want to see how each product's sales changed over time by drawing separate line charts side by side.
🎯 Goal: Create a pandas DataFrame with sales data, set up a configuration for the number of subplots, plot each product's sales as a separate line chart using subplots, and display the charts.
📋 What You'll Learn
Create a pandas DataFrame with sales data for three products over four months.
Create a variable to store the number of subplots (one for each product).
Use matplotlib subplots to create separate line charts for each product's sales.
Display the subplots with clear titles and labels.
💡 Why This Matters
🌍 Real World
Stores and businesses often track sales of different products over time. Visualizing each product's sales separately helps spot trends and compare performance clearly.
💼 Career
Data analysts and scientists use subplots to present multiple related charts in reports and dashboards, making data easier to understand for decision makers.
Progress0 / 4 steps
1
Create the sales data DataFrame
Create a pandas DataFrame called sales_data with the following data: months as the index with values 'Jan', 'Feb', 'Mar', 'Apr', and three columns 'Product_A', 'Product_B', 'Product_C' with sales values: [10, 15, 20, 25], [5, 7, 9, 11], and [12, 14, 16, 18] respectively.
Pandas
Need a hint?

Use pd.DataFrame with a dictionary for columns and index for months.

2
Set the number of subplots
Create a variable called num_plots and set it to the number of columns in sales_data.
Pandas
Need a hint?

Use sales_data.shape[1] to get the number of columns.

3
Create subplots and plot each product's sales
Import matplotlib.pyplot as plt. Use plt.subplots with num_plots rows and 1 column to create subplots. Use a for loop with variables idx and product to iterate over enumerate(sales_data.columns). Inside the loop, plot the sales data for each product on the corresponding subplot using axes[idx].plot. Set the title of each subplot to the product name using axes[idx].set_title(product). Label the x-axis as 'Month' and y-axis as 'Sales' for each subplot.
Pandas
Need a hint?

Use plt.subplots to create multiple plots and loop over columns with enumerate.

4
Display the subplots
Add a line to display the plots using plt.show().
Pandas
Need a hint?

Use plt.show() to display the figure window with all subplots.