Discover how to make your charts smarter and your work easier by mastering figure-level and axes-level methods!
Figure-level methods vs axes-level in Matplotlib - When to Use Which
Imagine you have many charts to make, each with different parts like titles, labels, and legends. You try to draw each chart by hand, adding every detail one by one for each small plot.
This manual way is slow and confusing. You might forget to add a label or place a legend wrongly. Managing many small plots separately can cause mistakes and wastes time.
Figure-level and axes-level methods help you organize your charts smartly. Figure-level methods handle the whole picture, while axes-level methods focus on each small plot. This way, you control details easily and avoid errors.
plt.subplot(1,2,1) plt.plot(data1) plt.title('Plot 1') plt.subplot(1,2,2) plt.plot(data2) plt.title('Plot 2')
fig, axs = plt.subplots(1, 2) axs[0].plot(data1) axs[0].set_title('Plot 1') axs[1].plot(data2) axs[1].set_title('Plot 2')
You can create complex, multi-part charts clearly and quickly, making your data stories easier to understand.
A scientist comparing two experiments side by side can use figure-level methods to arrange plots and axes-level methods to label each graph perfectly.
Manual plotting of multiple charts is slow and error-prone.
Figure-level methods manage the whole chart layout.
Axes-level methods control details inside each plot.
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
