Bird
Raised Fist0
Matplotlibdata~20 mins

Figure-level methods vs axes-level in Matplotlib - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Figure vs Axes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of figure-level vs axes-level plot commands

What will be the output of the following code snippet using matplotlib?

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 5, 6])
plt.title('My Title')
plt.show()

Note: plt.title() uses the pyplot interface acting on the current axes, ax.plot() is axes-level.

Matplotlib
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 5, 6])
plt.title('My Title')
plt.show()
AThe plot shows points connected by a line but no title is displayed.
BThe plot shows points connected by a line with the title 'My Title' displayed above the plot.
CThe plot is empty and the title 'My Title' is displayed above a blank figure.
DThe code raises an error because plt.title() cannot be used with ax.plot().
Attempts:
2 left
💡 Hint

plt.title() sets the title for the current axes, and ax is the current axes after plt.subplots() and ax.plot().

❓ Predict Output
intermediate
2:00remaining
Correct way to set title on axes-level plot

Given the following code, which option shows the correct way to add a title to the axes-level plot?

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 5, 6])
# Add title here
plt.show()
Matplotlib
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 5, 6])
# Add title here
plt.show()
Aax.title('My Plot')
Bfig.title('My Plot')
Cplt.title('My Plot')
Dax.set_title('My Plot')
Attempts:
2 left
💡 Hint

Axes objects have a method to set the title directly.

❓ data_output
advanced
2:00remaining
Number of axes created by figure-level vs axes-level methods

Consider the following code:

import seaborn as sns
import matplotlib.pyplot as plt

iris = sns.load_dataset('iris')

sns.scatterplot(data=iris, x='sepal_length', y='sepal_width')
plt.show()

How many axes are created and displayed by this code?

Matplotlib
import seaborn as sns
import matplotlib.pyplot as plt

iris = sns.load_dataset('iris')

sns.scatterplot(data=iris, x='sepal_length', y='sepal_width')
plt.show()
ANo axes are created; the plot is drawn directly on the figure.
BMultiple axes are created but only one is displayed.
COne axes is created and displayed with the scatter plot.
DTwo axes are created and displayed side by side.
Attempts:
2 left
💡 Hint

Seaborn figure-level functions create a figure and one axes by default.

🧠 Conceptual
advanced
2:00remaining
Difference between figure-level and axes-level methods

Which statement best describes the difference between figure-level and axes-level methods in matplotlib and seaborn?

AFigure-level methods create and manage the entire figure including multiple axes, while axes-level methods operate on a single axes within a figure.
BAxes-level methods create the figure and figure-level methods create individual plots inside axes.
CFigure-level methods only work with 3D plots, axes-level methods only work with 2D plots.
DAxes-level methods are used only for styling, figure-level methods are used only for plotting data.
Attempts:
2 left
💡 Hint

Think about what each method controls: the whole figure or just one plot area.

🔧 Debug
expert
3:00remaining
Why does the title not appear on the plot?

Examine the code below:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 5, 6])
fig.suptitle('Main Title')
ax.title('Sub Title')
plt.show()

Why does the plot show the main title but not the sub title?

Matplotlib
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 5, 6])
fig.suptitle('Main Title')
ax.title('Sub Title')
plt.show()
ABecause ax.title() is not a valid method; the correct method is ax.set_title().
BBecause fig.suptitle() overrides any axes titles.
CBecause the plot has no axes to set the title on.
DBecause plt.show() must be called before setting titles.
Attempts:
2 left
💡 Hint

Check the method names for setting titles on axes.

Practice

(1/5)
1. Which method would you use to set the main title for the entire figure in matplotlib?
easy
A. plt.title()
B. ax.set_title()
C. ax.set_xlabel()
D. fig.suptitle()

Solution

  1. Step 1: Understand figure-level vs axes-level

    Figure-level methods control the whole figure, axes-level methods control individual plots.
  2. Step 2: Identify method for main figure title

    The method fig.suptitle() sets the main title for the entire figure, while ax.set_title() sets title for one subplot.
  3. Final Answer:

    fig.suptitle() -> Option D
  4. Quick Check:

    Figure main title = fig.suptitle() [OK]
Hint: Main figure title uses fig.suptitle(), not ax.set_title() [OK]
Common Mistakes:
  • Using ax.set_title() for main figure title
  • Using plt.title() which affects current axes
  • Confusing xlabel with title
2. Which of the following is the correct syntax to set the x-axis label for a single plot (axes) in matplotlib?
easy
A. plt.set_xlabel('X Label')
B. fig.set_xlabel('X Label')
C. ax.set_xlabel('X Label')
D. fig.xlabel('X Label')

Solution

  1. Step 1: Identify the object controlling axis labels

    Axis labels belong to individual plots, so axes-level methods are used.
  2. Step 2: Choose correct method and object

    The method set_xlabel() is called on an axes object, so ax.set_xlabel('X Label') is correct.
  3. Final Answer:

    ax.set_xlabel('X Label') -> Option C
  4. Quick Check:

    Axis label = ax.set_xlabel() [OK]
Hint: Use ax.set_xlabel() for x-axis label, not fig or plt [OK]
Common Mistakes:
  • Calling set_xlabel() on figure object
  • Using plt.set_xlabel() which does not exist
  • Confusing axis label with figure title
3. What will the following code display?
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()
medium
A. A figure with main title 'Main Title' and one plot titled 'Plot Title' with labeled axes
B. Only a plot titled 'Main Title' with axes labeled 'X Axis' and 'Y Axis'
C. A figure with no titles but axes labeled 'X Axis' and 'Y Axis'
D. Error because fig.suptitle() cannot be used with ax.set_title()

Solution

  1. Step 1: Understand figure and axes titles

    fig.suptitle() sets the main figure title; ax.set_title() sets the subplot title.
  2. Step 2: Analyze axis labels and display

    ax.set_xlabel() and ax.set_ylabel() label the x and y axes of the plot. The code will show all these correctly.
  3. Final Answer:

    A figure with main title 'Main Title' and one plot titled 'Plot Title' with labeled axes -> Option A
  4. Quick 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]
Hint: fig.suptitle is main title; ax.set_title is subplot title [OK]
Common Mistakes:
  • Thinking fig.suptitle replaces ax.set_title
  • Expecting error when using both titles
  • Ignoring axis labels
4. Identify the error in this code snippet:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
fig.set_title('Main Title')
ax.set_xlabel('X Label')
plt.show()
medium
A. fig.set_title() is not a valid method for figure objects
B. ax.set_xlabel() should be called on fig, not ax
C. plt.show() must be called before setting titles
D. fig, ax = plt.subplots() returns only one object

Solution

  1. Step 1: Check figure method usage

    The figure object does not have a method called set_title(). The correct method for figure title is suptitle().
  2. Step 2: Verify other method calls

    ax.set_xlabel() is correct for labeling x-axis. plt.show() is correctly placed at the end.
  3. Final Answer:

    fig.set_title() is not a valid method for figure objects -> Option A
  4. Quick Check:

    Figure title method = fig.suptitle() not fig.set_title() [OK]
Hint: Figure title uses suptitle(), not set_title() [OK]
Common Mistakes:
  • Using fig.set_title() instead of fig.suptitle()
  • Confusing axes methods with figure methods
  • Calling plt.show() too early
5. You want to create a figure with 2 subplots side by side. You want a main title for the figure and individual titles for each subplot. Which code correctly achieves this?
hard
A. fig, axs = plt.subplots(1, 2) fig.title('Main Title') axs[0].title('Left Plot') axs[1].title('Right Plot') plt.show()
B. fig, axs = plt.subplots(1, 2) fig.suptitle('Main Title') axs[0].set_title('Left Plot') axs[1].set_title('Right Plot') plt.show()
C. fig, axs = plt.subplots(2, 1) fig.set_title('Main Title') axs[0].set_xlabel('Left Plot') axs[1].set_xlabel('Right Plot') plt.show()
D. fig, axs = plt.subplots(1, 2) axs.set_title('Main Title') fig.set_title('Left Plot') fig.set_title('Right Plot') plt.show()

Solution

  1. 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.
  2. Step 2: Set individual subplot titles correctly

    Each subplot is accessed by axs[0] and axs[1]. Use set_title() to set their titles.
  3. Step 3: Verify other options are incorrect

    The other options misuse methods like fig.set_title() or fig.title() which do not exist or use wrong subplot layout.
  4. 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 B
  5. Quick Check:

    Figure main title = fig.suptitle(), subplot titles = axs[i].set_title() [OK]
Hint: Use fig.suptitle() for main title, axs[i].set_title() for subplots [OK]
Common Mistakes:
  • Using fig.set_title() instead of fig.suptitle()
  • Calling title() instead of set_title()
  • Wrong subplot layout dimensions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard