0
0
Matplotlibdata~15 mins

Plt.subplots with rows and columns in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Create a Grid of Plots Using plt.subplots with Rows and Columns
📖 Scenario: You are a data analyst who needs to visualize multiple sets of data side by side. To do this neatly, you want to create a grid of plots using matplotlib.
🎯 Goal: Build a 2 by 2 grid of plots using plt.subplots with rows and columns, then plot simple line charts on each subplot.
📋 What You'll Learn
Use plt.subplots to create a figure with 2 rows and 2 columns of subplots
Plot a simple line on each subplot using the provided data
Use the variables fig and axes exactly as shown
Print the figure and axes objects to confirm creation
💡 Why This Matters
🌍 Real World
Creating multiple plots in a grid helps compare different data sets side by side, like sales over months for different products.
💼 Career
Data scientists and analysts often use subplot grids to present multiple visual insights clearly in reports and presentations.
Progress0 / 4 steps
1
Create a 2x2 grid of subplots
Write code to create a figure and a 2 by 2 grid of subplots using plt.subplots. Assign the outputs to variables fig and axes.
Matplotlib
Need a hint?

Use plt.subplots(nrows=2, ncols=2) and assign the result to fig, axes.

2
Prepare data for plotting
Create a list called data containing four lists of numbers: [1, 2, 3], [3, 2, 1], [1, 3, 2], and [2, 1, 3].
Matplotlib
Need a hint?

Make a list named data with exactly these four lists inside.

3
Plot each data list on the subplots
Use a for loop with variables ax and d to iterate over axes.flat and data simultaneously. Inside the loop, call ax.plot(d) to plot the data on each subplot.
Matplotlib
Need a hint?

Use zip(axes.flat, data) to loop over both together and plot each data list on its subplot.

4
Display the figure and print objects
Print the variables fig and axes on separate lines. Then call plt.show() to display the plots.
Matplotlib
Need a hint?

Use print(fig) and print(axes) to show the objects, then plt.show() to display the figure.