Figure-level and axes-level methods help you control different parts of a plot. Figure-level methods manage the whole picture, while axes-level methods focus on smaller parts inside it.
Figure-level methods vs axes-level in Matplotlib
import matplotlib.pyplot as plt # Figure-level method example fig = plt.figure(figsize=(8, 6)) fig.suptitle('Main Title for Whole Figure') # Axes-level method example ax = fig.add_subplot(1, 1, 1) ax.plot([1, 2, 3], [4, 5, 6]) ax.set_title('Title for One Chart') ax.set_xlabel('X axis label') ax.set_ylabel('Y axis label')
Figure-level methods affect the entire figure, which can contain one or more charts (axes).
Axes-level methods affect only one chart inside the figure.
fig = plt.figure()
fig.suptitle('Big Title')ax = fig.add_subplot(1, 1, 1) ax.plot([1, 2, 3], [3, 2, 1]) ax.set_title('Small Chart Title')
fig, axs = plt.subplots(2, 1) fig.suptitle('Figure Title') axs[0].set_title('Top Chart') axs[1].set_title('Bottom Chart')
This code creates one figure with two charts side by side. The figure has a main title, and each chart has its own smaller title and labels.
import matplotlib.pyplot as plt # Create a figure with two charts fig, axs = plt.subplots(1, 2, figsize=(10, 4)) # Set a title for the whole figure fig.suptitle('Figure-level Title: Two Charts') # First chart: simple line plot axs[0].plot([1, 2, 3], [1, 4, 9]) axs[0].set_title('Axes-level Title: Chart 1') axs[0].set_xlabel('X axis') axs[0].set_ylabel('Y axis') # Second chart: simple bar plot axs[1].bar([1, 2, 3], [3, 2, 5]) axs[1].set_title('Axes-level Title: Chart 2') axs[1].set_xlabel('Categories') axs[1].set_ylabel('Values') # Show the plot plt.show()
Figure-level methods like fig.suptitle() set properties for the whole figure.
Axes-level methods like ax.set_title() set properties for individual charts.
Using both lets you organize complex plots clearly.
Figure-level methods control the entire plot area.
Axes-level methods control individual charts inside the plot.
Use figure-level for overall titles and size, axes-level for chart details.
Practice
Solution
Step 1: Understand figure-level vs axes-level
Figure-level methods control the whole figure, axes-level methods control individual plots.Step 2: Identify method for main figure title
The methodfig.suptitle()sets the main title for the entire figure, whileax.set_title()sets title for one subplot.Final Answer:
fig.suptitle() -> Option DQuick Check:
Figure main title = fig.suptitle() [OK]
- Using ax.set_title() for main figure title
- Using plt.title() which affects current axes
- Confusing xlabel with title
Solution
Step 1: Identify the object controlling axis labels
Axis labels belong to individual plots, so axes-level methods are used.Step 2: Choose correct method and object
The methodset_xlabel()is called on an axes object, soax.set_xlabel('X Label')is correct.Final Answer:
ax.set_xlabel('X Label') -> Option CQuick Check:
Axis label = ax.set_xlabel() [OK]
- Calling set_xlabel() on figure object
- Using plt.set_xlabel() which does not exist
- Confusing axis label with figure title
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
fig.suptitle('Main Title')
ax.set_title('Plot Title')
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
plt.show()Solution
Step 1: Understand figure and axes titles
fig.suptitle()sets the main figure title;ax.set_title()sets the subplot title.Step 2: Analyze axis labels and display
ax.set_xlabel()andax.set_ylabel()label the x and y axes of the plot. The code will show all these correctly.Final Answer:
A figure with main title 'Main Title' and one plot titled 'Plot Title' with labeled axes -> Option AQuick Check:
fig.suptitle + ax.set_title + ax labels = A figure with main title 'Main Title' and one plot titled 'Plot Title' with labeled axes [OK]
- Thinking fig.suptitle replaces ax.set_title
- Expecting error when using both titles
- Ignoring axis labels
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
fig.set_title('Main Title')
ax.set_xlabel('X Label')
plt.show()Solution
Step 1: Check figure method usage
The figure object does not have a method calledset_title(). The correct method for figure title issuptitle().Step 2: Verify other method calls
ax.set_xlabel()is correct for labeling x-axis.plt.show()is correctly placed at the end.Final Answer:
fig.set_title() is not a valid method for figure objects -> Option AQuick Check:
Figure title method = fig.suptitle() not fig.set_title() [OK]
- Using fig.set_title() instead of fig.suptitle()
- Confusing axes methods with figure methods
- Calling plt.show() too early
Solution
Step 1: Create subplots and set main figure title
plt.subplots(1, 2)creates two side-by-side plots.fig.suptitle()sets the main figure title.Step 2: Set individual subplot titles correctly
Each subplot is accessed byaxs[0]andaxs[1]. Useset_title()to set their titles.Step 3: Verify other options are incorrect
The other options misuse methods likefig.set_title()orfig.title()which do not exist or use wrong subplot layout.Final Answer:
fig, axs = plt.subplots(1, 2) fig.suptitle('Main Title') axs[0].set_title('Left Plot') axs[1].set_title('Right Plot') plt.show() -> Option BQuick Check:
Figure main title = fig.suptitle(), subplot titles = axs[i].set_title() [OK]
- Using fig.set_title() instead of fig.suptitle()
- Calling title() instead of set_title()
- Wrong subplot layout dimensions
Ready to prove it?
Pass the test and this pattern moves unknown
