0
0
Matplotlibdata~15 mins

Unequal subplot sizes in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Unequal subplot sizes
📖 Scenario: You are creating a simple data visualization for a report. You want to show two charts side by side, but one chart should be bigger than the other to highlight its importance.
🎯 Goal: Build a matplotlib figure with two subplots arranged horizontally. The left subplot should be twice as wide as the right subplot.
📋 What You'll Learn
Create a matplotlib figure with two subplots arranged in one row
Use gridspec_kw to set the width ratios of the subplots to 2 and 1
Plot simple line charts on each subplot
Display the figure with plt.show()
💡 Why This Matters
🌍 Real World
In reports or presentations, some charts need more space to show details clearly. Unequal subplot sizes help highlight important data visually.
💼 Career
Data analysts and scientists often customize plots to communicate insights effectively. Knowing how to adjust subplot sizes is a useful skill.
Progress0 / 4 steps
1
Create sample data
Create two lists called x and y1 with values [1, 2, 3, 4, 5] and [1, 4, 9, 16, 25] respectively.
Matplotlib
Need a hint?

Use square brackets to create lists. For example, x = [1, 2, 3].

2
Add second data list
Create a list called y2 with values [25, 16, 9, 4, 1].
Matplotlib
Need a hint?

Remember to use square brackets and commas to separate values.

3
Create figure with unequal subplot sizes
Import matplotlib.pyplot as plt. Create a figure and two subplots in one row using plt.subplots() with nrows=1, ncols=2, and gridspec_kw={'width_ratios': [2, 1]}. Plot x vs y1 on the first subplot and x vs y2 on the second subplot.
Matplotlib
Need a hint?

Use plt.subplots() with gridspec_kw to set different widths for subplots.

4
Display the figure
Add a line to display the figure using plt.show().
Matplotlib
Need a hint?

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