What if you could create multiple charts perfectly aligned with just one simple line of code?
Why Fig, ax = plt.subplots pattern in Matplotlib? - Purpose & Use Cases
Imagine you want to draw several charts to compare data side by side. You try to create each chart separately and then arrange them manually on your screen or paper.
This manual way is slow and confusing. You might lose track of which chart belongs where. Adjusting sizes or adding titles to each chart becomes a headache. It's easy to make mistakes and hard to fix them quickly.
The fig, ax = plt.subplots() pattern helps you create one or many charts in a clean, organized way. It gives you control over each chart (called an axis) while keeping them all inside one figure. This makes drawing, labeling, and arranging charts simple and error-free.
plt.figure() plt.plot(data1) plt.figure() plt.plot(data2)
fig, ax = plt.subplots(1, 2) ax[0].plot(data1) ax[1].plot(data2)
It lets you build clear, neat, and flexible multi-chart layouts that are easy to update and understand.
Think of a weather report showing temperature and rainfall side by side. Using fig, ax = plt.subplots(), you can create both charts together, label them properly, and adjust their sizes to fit perfectly on one page.
Manual chart creation is slow and messy.
fig, ax = plt.subplots() organizes multiple charts in one figure.
This pattern makes plotting easier, clearer, and more flexible.
Practice
fig represent when you write fig, ax = plt.subplots() in matplotlib?Solution
Step 1: Understand the role of
Thefiginplt.subplots()figvariable holds the entire figure container, which can include multiple plots, titles, legends, and other elements.Step 2: Differentiate
Thefigfromaxaxvariable is the specific plot area inside the figure where data is drawn, not the whole container.Final Answer:
The whole figure container that holds all plot elements -> Option BQuick Check:
fig= whole figure container [OK]
- Confusing fig with ax as the plot area
- Thinking fig is just the plot title
- Assuming fig is a list of plots
plt.subplots()?Solution
Step 1: Recall the correct unpacking syntax
The functionplt.subplots()returns two objects: the figure and the axes. The correct way to capture them isfig, ax = plt.subplots().Step 2: Check each option for syntax correctness
fig = plt.subplots(ax) tries to passaxas an argument incorrectly. ax, fig = plt.subplots() reverses the order, which is wrong. plt.subplots = fig, ax tries to assign incorrectly.Final Answer:
fig, ax = plt.subplots() -> Option DQuick Check:
Correct unpacking = fig, ax = plt.subplots() [OK]
- Swapping fig and ax order
- Passing ax as argument to plt.subplots()
- Incorrect assignment syntax
ax after running this code?import matplotlib.pyplot as plt fig, ax = plt.subplots()
Solution
Step 1: Understand what plt.subplots() returns
The function returns a figure object and an axes object. The axes object is usually an instance ofAxesSubplotclass.Step 2: Identify the type of
After unpacking,axaxis the axes object, which is of typematplotlib.axes._subplots.AxesSubplot.Final Answer:
<class 'matplotlib.axes._subplots.AxesSubplot'> -> Option AQuick Check:
ax type = AxesSubplot [OK]
- Confusing ax type with fig type
- Thinking ax is a list or tuple
- Assuming ax is a string or number
import matplotlib.pyplot as plt fig, ax = plt.subplots ax.plot([1, 2, 3], [4, 5, 6]) plt.show()
Solution
Step 1: Check the call to plt.subplots
The code usesplt.subplotswithout parentheses, so it assigns the function itself, not its result.Step 2: Understand the impact on ax.plot
Sincefig, axare assigned incorrectly,axis a function, not an axes object, soax.plot()will cause an error.Final Answer:
Missing parentheses after plt.subplots -> Option CQuick Check:
Call plt.subplots() with () [OK]
- Forgetting parentheses on plt.subplots
- Mixing up fig and ax variables
- Calling plt.show() too early
plt.subplots(). Which code correctly creates the figure and axes, and plots a line on the second subplot?Solution
Step 1: Create figure with 2 rows and 1 column
Useplt.subplots(2, 1)to create 2 vertical subplots. This returnsaxas an array of axes.Step 2: Plot on the second subplot
Access the second subplot withax[1](indexing starts at 0) and callplot()on it.Final Answer:
fig, ax = plt.subplots(2, 1) ax[1].plot([1, 2, 3], [3, 2, 1]) -> Option AQuick Check:
2 rows, 1 col = plt.subplots(2,1), plot on ax[1] [OK]
- Using wrong subplot shape (1,2) instead of (2,1)
- Trying to plot on ax without indexing when multiple axes exist
- Using invalid index like ax[2] when only 2 subplots exist
Ready to prove it?
Pass the test and this pattern moves unknown
