0
0
Matplotlibdata~30 mins

Individual subplot customization in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Individual subplot customization
📖 Scenario: You are analyzing sales data for two different products over four quarters. You want to visualize the sales trends side by side using two subplots. Each subplot should have its own title and y-axis label to clearly show which product's sales are being displayed.
🎯 Goal: Create a figure with two subplots side by side. Plot the sales data for Product A in the first subplot and Product B in the second subplot. Customize each subplot with its own title and y-axis label.
📋 What You'll Learn
Create a list called quarters with values [1, 2, 3, 4].
Create two lists called sales_a and sales_b with sales data for Product A and Product B respectively.
Create a figure with two subplots side by side using plt.subplots() with 1 row and 2 columns.
Plot sales_a against quarters in the first subplot.
Plot sales_b against quarters in the second subplot.
Set the title of the first subplot to 'Product A Sales' and the y-axis label to 'Sales (units)'.
Set the title of the second subplot to 'Product B Sales' and the y-axis label to 'Sales (units)'.
Display the plot using plt.show().
💡 Why This Matters
🌍 Real World
Visualizing multiple related datasets side by side helps compare trends easily, such as sales of different products over time.
💼 Career
Data scientists and analysts often create customized subplots to present clear and informative visual reports to stakeholders.
Progress0 / 4 steps
1
Create sales data lists
Create a list called quarters with values [1, 2, 3, 4]. Create a list called sales_a with values [150, 200, 250, 300] for Product A sales. Create a list called sales_b with values [180, 210, 260, 310] for Product B sales.
Matplotlib
Need a hint?

Use square brackets to create lists with the exact values given.

2
Create subplots figure
Import matplotlib.pyplot as plt. Create a figure and two subplots side by side using plt.subplots() with 1 row and 2 columns. Store the axes in a variable called axes.
Matplotlib
Need a hint?

Use plt.subplots(1, 2) to create 1 row and 2 columns of subplots.

3
Plot sales data and customize subplots
Plot sales_a against quarters on the first subplot using axes[0].plot(). Plot sales_b against quarters on the second subplot using axes[1].plot(). Set the title of the first subplot to 'Product A Sales' using axes[0].set_title(). Set the y-axis label of the first subplot to 'Sales (units)' using axes[0].set_ylabel(). Set the title of the second subplot to 'Product B Sales' using axes[1].set_title(). Set the y-axis label of the second subplot to 'Sales (units)' using axes[1].set_ylabel().
Matplotlib
Need a hint?

Use axes[index].plot() to plot on each subplot. Use set_title() and set_ylabel() to customize each subplot.

4
Display the plot
Use plt.show() to display the figure with the two customized subplots.
Matplotlib
Need a hint?

Call plt.show() to display the figure window with your plots.