What does the plt.subplots() function return in matplotlib?
It returns two objects: fig, which is the Figure object representing the entire window or page, and ax, which is the Axes object where the actual plot is drawn.
Click to reveal answer
beginner
Why do we use fig, ax = plt.subplots() instead of just plt.plot()?
Using fig, ax = plt.subplots() gives more control over the plot. You can customize the figure size, add multiple plots, and modify axes properties easily.
Click to reveal answer
intermediate
How can you create multiple plots in one figure using plt.subplots()?
You can pass the number of rows and columns as arguments, like fig, axs = plt.subplots(2, 2). This creates a grid of 4 plots, and axs is an array of Axes objects.
Click to reveal answer
beginner
What is the role of the fig object in the fig, ax = plt.subplots() pattern?
The fig object represents the whole figure or window. You can use it to set the figure size, save the figure to a file, or adjust layout settings.
Click to reveal answer
beginner
How do you plot data on the axes object returned by plt.subplots()?
You call plotting methods on the ax object, like ax.plot(x, y). This draws the plot inside the axes area of the figure.
Click to reveal answer
What does ax represent in fig, ax = plt.subplots()?
AThe entire figure window
BA color map
CA data array
DThe plotting area or axes
✗ Incorrect
ax is the axes object where the plot is drawn, not the whole figure.
How do you create a 3x1 grid of plots using plt.subplots()?
A<code>fig, axs = plt.subplots(3)</code>
B<code>fig, ax = plt.subplots(1, 3)</code>
C<code>fig, axs = plt.subplots(3, 1)</code>
D<code>fig, ax = plt.subplots(1)</code>
✗ Incorrect
Passing rows=3 and columns=1 creates a vertical stack of 3 plots.
Which object do you use to save the plot to a file?
Aax
Bplt
Cfig
Ddata
✗ Incorrect
The fig object has the savefig() method to save the figure.
What is the default number of plots created by plt.subplots() with no arguments?
A1
B0
C2
D4
✗ Incorrect
By default, it creates one figure and one axes (one plot).
How do you plot a line graph on the axes object?
A<code>ax.plot(x, y)</code>
B<code>fig.plot(x, y)</code>
C<code>plt.plot(x, y)</code>
D<code>ax.show(x, y)</code>
✗ Incorrect
You use ax.plot(x, y) to plot on the specific axes.
Explain the purpose of the fig and ax objects returned by plt.subplots().
Think about the difference between the whole window and the area where you draw.
You got /4 concepts.
Describe how to create multiple plots in one figure using plt.subplots() and how to access each plot.
Consider a grid of small plots inside one big window.
You got /4 concepts.
Practice
(1/5)
1. What does the variable fig represent when you write fig, ax = plt.subplots() in matplotlib?
easy
A. The specific plot area where data is drawn
B. The whole figure container that holds all plot elements
C. A list of all plots in the figure
D. The title of the plot
Solution
Step 1: Understand the role of fig in plt.subplots()
The fig variable holds the entire figure container, which can include multiple plots, titles, legends, and other elements.
Step 2: Differentiate fig from ax
The ax variable 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 B
Quick Check:
fig = whole figure container [OK]
Hint: Remember: fig = full figure, ax = plot area [OK]
Common Mistakes:
Confusing fig with ax as the plot area
Thinking fig is just the plot title
Assuming fig is a list of plots
2. Which of the following is the correct syntax to create a figure and axes using matplotlib's plt.subplots()?
easy
A. plt.subplots = fig, ax
B. fig = plt.subplots(ax)
C. ax, fig = plt.subplots()
D. fig, ax = plt.subplots()
Solution
Step 1: Recall the correct unpacking syntax
The function plt.subplots() returns two objects: the figure and the axes. The correct way to capture them is fig, ax = plt.subplots().
Step 2: Check each option for syntax correctness
fig = plt.subplots(ax) tries to pass ax as 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 D
Quick Check:
Correct unpacking = fig, ax = plt.subplots() [OK]
Hint: Remember: fig first, then ax from plt.subplots() [OK]
Common Mistakes:
Swapping fig and ax order
Passing ax as argument to plt.subplots()
Incorrect assignment syntax
3. What will be the output type of ax after running this code?
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
medium
A.
B.
C.
D.
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 of AxesSubplot class.
Step 2: Identify the type of ax
After unpacking, ax is the axes object, which is of type matplotlib.axes._subplots.AxesSubplot.
Final Answer:
<class 'matplotlib.axes._subplots.AxesSubplot'> -> Option A
Quick Check:
ax type = AxesSubplot [OK]
Hint: ax is the plot area object, not the figure [OK]
The code uses plt.subplots without parentheses, so it assigns the function itself, not its result.
Step 2: Understand the impact on ax.plot
Since fig, ax are assigned incorrectly, ax is a function, not an axes object, so ax.plot() will cause an error.
Final Answer:
Missing parentheses after plt.subplots -> Option C
Quick Check:
Call plt.subplots() with () [OK]
Hint: Always add () to call plt.subplots function [OK]
Common Mistakes:
Forgetting parentheses on plt.subplots
Mixing up fig and ax variables
Calling plt.show() too early
5. You want to create a figure with 2 rows and 1 column of plots using plt.subplots(). Which code correctly creates the figure and axes, and plots a line on the second subplot?