This pattern helps you create a figure and one or more plots (axes) in a clear and organized way.
Fig, ax = plt.subplots pattern in Matplotlib
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(width, height))
fig is the whole figure or window that holds your plots.
ax is the plot area where you draw your data.
fig, ax = plt.subplots()
fig, ax = plt.subplots(figsize=(8, 6))
axs is a 2D array of axes.fig, axs = plt.subplots(2, 2)
fig, ax = plt.subplots(nrows=3, ncols=1, figsize=(5, 10))
This code creates a figure with one plot. It draws a line connecting points (1,4), (2,5), and (3,6). Then it adds a title and labels for the axes. Finally, it shows the plot window.
import matplotlib.pyplot as plt # Create a figure and one plot fig, ax = plt.subplots(figsize=(6, 4)) # Draw a simple line ax.plot([1, 2, 3], [4, 5, 6]) # Add title and labels ax.set_title('Simple Line Plot') ax.set_xlabel('X axis') ax.set_ylabel('Y axis') # Show the plot plt.show()
If you create multiple plots, ax becomes an array or matrix of axes objects.
You can use fig to adjust the whole figure, like saving it or changing layout.
Always call plt.show() to display your plots when running scripts.
The fig, ax = plt.subplots() pattern creates a figure and plot(s) together.
fig is the container; ax is where you draw your data.
This pattern helps organize and customize your charts easily.
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
