We use the Figure and Axes model to organize and control plots clearly. It helps us put one or many charts in a single window.
Figure and Axes mental model in Matplotlib
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(width, height))
fig is the whole window or figure that holds one or more plots.
ax is the actual plot area where data is drawn.
fig, ax = plt.subplots() ax.plot([1, 2, 3], [4, 5, 6])
fig, axs = plt.subplots(2, 2) axs[0, 0].plot([1, 2], [3, 4]) axs[1, 1].plot([5, 6], [7, 8])
fig, ax = plt.subplots(figsize=(8, 4)) ax.bar(['A', 'B'], [10, 20])
This code creates one window (figure) with two plots (axes) side by side. The first plot shows a line chart, and the second shows a scatter chart. We add titles to each plot and a main title for the whole window.
import matplotlib.pyplot as plt # Create a figure with 2 plots side by side fig, axs = plt.subplots(1, 2, figsize=(10, 4)) # First plot: line chart axs[0].plot([1, 2, 3], [1, 4, 9]) axs[0].set_title('Line Chart') # Second plot: scatter chart axs[1].scatter([1, 2, 3], [9, 4, 1]) axs[1].set_title('Scatter Chart') # Add a main title for the whole figure fig.suptitle('Figure with Two Axes') # Show the plot window plt.show()
Think of Figure as a picture frame and Axes as the canvas inside where you paint your data.
You can have many Axes inside one Figure to compare different charts easily.
Use figsize to control the size of the whole Figure window.
The Figure is the whole window or image that holds your plots.
The Axes is the area where the actual data is drawn.
You can create multiple Axes inside one Figure to show many plots together.
Practice
Figure represent in matplotlib's Figure and Axes model?Solution
Step 1: Understand the role of Figure
The Figure is the whole container or window that holds one or more plots.Step 2: Differentiate Figure from Axes
Axes is the part inside the Figure where data is drawn, not the entire window.Final Answer:
The entire image or window that contains all plots -> Option CQuick Check:
Figure = whole window [OK]
- Confusing Figure with Axes
- Thinking Figure is a single plot line
- Mixing Figure with plot colors
Solution
Step 1: Recall matplotlib syntax
The standard way to create Figure and Axes together is usingplt.subplots().Step 2: Check each option
fig, ax = plt.subplots() correctly assigns Figure and Axes fromplt.subplots(). Others misuse functions or assign incorrectly.Final Answer:
fig, ax = plt.subplots() -> Option AQuick Check:
Use plt.subplots() to get Figure and Axes [OK]
- Using plt.plot() to create Axes
- Confusing plt.figure() with plt.subplots()
- Trying non-existent functions like plt.create_figure()
ax after running this code?import matplotlib.pyplot as plt fig, ax = plt.subplots()
Solution
Step 1: Understand plt.subplots() return values
It returns a tuple: (Figure object, Axes object or array of Axes).Step 2: Check variable assignment
Here,axis assigned the second element, which is a single Axes object.Final Answer:
A matplotlib Axes object -> Option BQuick Check:
ax = Axes object from plt.subplots() [OK]
- Thinking ax is a Figure
- Assuming ax is a list without multiple plots
- Confusing Axes with NumPy arrays
import matplotlib.pyplot as plt fig = plt.figure() ax = plt.axes() ax.plot([1, 2, 3], [4, 5, 6]) plt.show()
Solution
Step 1: Understand plt.figure() and plt.axes()
plt.figure()creates a Figure and sets it as current.plt.axes()creates Axes in the current Figure using default positioning.Step 2: Verify code execution
The plot is correctly added toaxandplt.show()displays the figure. The code runs without errors.Final Answer:
No error, code runs fine -> Option DQuick Check:
plt.axes() adds to current Figure [OK]
- Calling plt.axes() without linking to Figure
- Assuming plt.axes() auto-links to last Figure
- Calling plt.figure() after plt.axes()
Solution
Step 1: Understand subplots with shared axes
Usingplt.subplots(1, 2, sharey=True)creates one row, two columns of Axes sharing the y-axis.Step 2: Check code correctness
fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True) ax1.plot([1,2,3], [4,5,6]) ax2.plot([1,2,3], [6,5,4]):fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True) ax1.plot([1,2,3], [4,5,6]) ax2.plot([1,2,3], [6,5,4])correctly unpacks two Axes and plots data with shared y-axis. fig, ax = plt.subplots(1, 2) ax[0].plot([1,2,3], [4,5,6]) ax[1].plot([1,2,3], [6,5,4]) omitssharey=True. fig = plt.figure() ax1 = fig.add_subplot(1, 2, 1) ax2 = fig.add_subplot(1, 2, 2) ax1.sharey(ax2) calls non-existentsharey()method. fig, ax = plt.subplots(2, 1) ax[0].plot([1,2,3], [4,5,6]) ax[1].plot([1,2,3], [6,5,4]) creates vertical subplots.Final Answer:
fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True) ax1.plot([1,2,3], [4,5,6]) ax2.plot([1,2,3], [6,5,4]) -> Option AQuick Check:
Use sharey=True in plt.subplots() for shared y-axis [OK]
- Trying to call sharey() on Axes objects
- Creating vertical subplots instead of horizontal
- Not unpacking multiple Axes correctly
Ready to prove it?
Pass the test and this pattern moves unknown
