0
0
Matplotlibdata~20 mins

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

Choose your learning style9 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.