What if you could arrange multiple charts perfectly every time without messy guesswork?
Why Figure and Axes mental model in Matplotlib? - Purpose & Use Cases
Imagine you want to draw multiple charts on a single page by hand, like sketching graphs on paper without any guides or frames.
You try to place each chart exactly where you want, but it's hard to keep them aligned and sized properly.
Doing this manually is slow and frustrating because you must measure and adjust each chart's position and size by trial and error.
It's easy to overlap charts or leave too much empty space, making your page look messy and confusing.
The Figure and Axes mental model in matplotlib helps by giving you a clear structure: the Figure is like the whole page, and Axes are the individual charts inside it.
This way, you can easily control where each chart goes and how big it is, without guesswork.
plt.plot(x, y)
plt.plot(x2, y2) # hard to control layoutfig, ax = plt.subplots(2) ax[0].plot(x, y) ax[1].plot(x2, y2) # clear layout control
You can create clean, organized multi-chart layouts that are easy to read and customize.
When making a report with sales data, you can put a bar chart and a line chart side by side, each with its own labels and size, all neatly arranged on one page.
Figure is the whole drawing area or page.
Axes are the individual charts inside the Figure.
This model helps organize multiple charts clearly and easily.
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
