0
0
Matplotlibdata~20 mins

Why the OO interface matters in Matplotlib - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Matplotlib OO Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of plot commands using OO interface
What will be the output of this code snippet using matplotlib's OO interface?
Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 5, 6])
ax.set_title('Test Plot')
plt.show()
AAn empty plot with no lines and no title
BA line plot with points (1,4), (2,5), (3,6) and title 'Test Plot' displayed
CA scatter plot with points (1,4), (2,5), (3,6) but no title
DA bar chart with bars at x=1,2,3 with heights 4,5,6 and title 'Test Plot'
Attempts:
2 left
💡 Hint
Remember that ax.plot creates a line plot and ax.set_title sets the title for that axes.
🧠 Conceptual
intermediate
1:30remaining
Why prefer OO interface over pyplot state-machine?
Which reason best explains why the OO interface is preferred over the pyplot state-machine interface in matplotlib?
AIt allows explicit control over multiple figures and axes, avoiding confusion in complex plots
BIt automatically chooses the best colors for plots without user input
CIt requires less code to create simple plots
DIt disables interactive plotting features
Attempts:
2 left
💡 Hint
Think about managing multiple plots at once.
🔧 Debug
advanced
2:00remaining
Identify the error in this OO interface code
What error will this code raise when run?
Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 5])
ax.set_title('Mismatch Data')
plt.show()
AValueError: x and y must have same first dimension
BTypeError: 'AxesSubplot' object is not callable
CNo error, plot displays correctly
DAttributeError: 'AxesSubplot' object has no attribute 'set_title'
Attempts:
2 left
💡 Hint
Check the lengths of x and y data passed to plot.
data_output
advanced
1:30remaining
Number of axes created with subplots
What is the shape and number of axes objects created by this code?
Matplotlib
import matplotlib.pyplot as plt
fig, axes = plt.subplots(2, 3)
print(type(axes), axes.shape)
A<class 'tuple'> (2, 3)
B<class 'matplotlib.axes._subplots.AxesSubplot'> (6,)
C<class 'list'> (2, 3)
D<class 'numpy.ndarray'> (2, 3)
Attempts:
2 left
💡 Hint
plt.subplots returns a numpy array of axes when rows and columns are more than 1.
🚀 Application
expert
2:30remaining
Correctly update title for specific subplot
Given a 2x2 grid of subplots, which code correctly sets the title of the bottom-right subplot only?
Matplotlib
import matplotlib.pyplot as plt
fig, axes = plt.subplots(2, 2)
# Set title for bottom-right subplot here
Afig.axes[3].set_title('Bottom Right')
Baxes[2, 2].set_title('Bottom Right')
Caxes[1, 1].set_title('Bottom Right')
Daxes[1].set_title('Bottom Right')
Attempts:
2 left
💡 Hint
Remember Python uses zero-based indexing for arrays.