0
0
Pandasdata~10 mins

Subplots for multiple charts in Pandas - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Subplots for multiple charts
Create Figure and Axes with subplots()
Prepare Data for each chart
Plot data on each Axes
Adjust layout for clarity
Show or save the combined figure
This flow shows how to create multiple charts in one figure using subplots, plot data on each, and display them together.
Execution Sample
Pandas
import pandas as pd
import matplotlib.pyplot as plt

fig, axs = plt.subplots(1, 2)
data = pd.DataFrame({'A': [1,2,3], 'B': [4,5,6]})
axs[0].plot(data['A'])
axs[1].plot(data['B'])
plt.show()
This code creates a figure with two side-by-side charts plotting columns 'A' and 'B' from a DataFrame.
Execution Table
StepActionCode/MethodResult/State
1Import pandas and matplotlib.pyplotimport pandas as pd; import matplotlib.pyplot as pltLibraries ready for use
2Create figure and axes with 1 row, 2 columnsfig, axs = plt.subplots(1, 2)Figure with 2 Axes objects created
3Create DataFrame with columns A and Bdata = pd.DataFrame({'A': [1,2,3], 'B': [4,5,6]})DataFrame with 3 rows and 2 columns
4Plot column 'A' on first Axesaxs[0].plot(data['A'])Line chart of [1,2,3] on left subplot
5Plot column 'B' on second Axesaxs[1].plot(data['B'])Line chart of [4,5,6] on right subplot
6Display the figure with both subplotsplt.show()Window opens showing two side-by-side line charts
7End of executionN/AAll charts displayed successfully
💡 Execution stops after displaying the figure with all subplots.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
figNoneFigure object createdFigure object unchangedFigure with first plotFigure with two plotsFigure with two plots
axsNoneArray of 2 Axes objectsUnchangedFirst Axes has plotBoth Axes have plotsBoth Axes have plots
dataNoneNoneDataFrame with columns A and BUnchangedUnchangedUnchanged
Key Moments - 3 Insights
Why do we use axs[0] and axs[1] to plot instead of just axs?
Because plt.subplots(1, 2) returns an array of Axes objects, each subplot is accessed by its index like axs[0] for the first chart and axs[1] for the second, as shown in execution_table steps 2, 4, and 5.
What happens if we try to plot data directly on fig instead of axs?
The figure object 'fig' does not have a plot method; plotting must be done on Axes objects (axs). This is why in steps 4 and 5, plotting is done on axs[0] and axs[1], not on fig.
How does plt.show() know to display both charts together?
Because both plots are drawn on subplots within the same figure 'fig', calling plt.show() in step 6 displays the entire figure with all its subplots.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the state of 'axs' after step 2?
AAn array of 2 Axes objects
BA single Axes object
CNone
DA DataFrame
💡 Hint
Check the 'Result/State' column for step 2 in the execution_table.
At which step does the first plot appear on the figure?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Look at the 'Action' and 'Result/State' columns for when plotting on axs[0] happens.
If we change plt.subplots(1, 2) to plt.subplots(2, 1), how would the layout change?
ATwo charts side by side
BOnly one chart shown
CTwo charts stacked vertically
DNo charts shown
💡 Hint
Recall that plt.subplots(rows, columns) defines the grid layout of subplots.
Concept Snapshot
Use plt.subplots(rows, cols) to create multiple charts in one figure.
Access each subplot via the returned Axes array (e.g., axs[0], axs[1]).
Plot data on each Axes separately.
Call plt.show() once to display all charts together.
Adjust layout if needed for clarity.
Full Transcript
This lesson shows how to create multiple charts in one figure using pandas and matplotlib. First, we import the needed libraries. Then, we create a figure and multiple subplots using plt.subplots. We prepare a DataFrame with data columns. Each column is plotted on a separate subplot accessed by index. Finally, we display the combined figure with all charts using plt.show. Key points include accessing subplots via axs array and plotting on each separately. The execution table traces each step from creating the figure to showing the plots. The variable tracker shows how fig, axs, and data change. Common confusions about subplot indexing and plotting on figure vs axes are clarified. The quiz tests understanding of subplot creation, plotting steps, and layout changes.