0
0
Matplotlibdata~20 mins

Axes vs pyplot interface comparison in Matplotlib - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Axes vs Pyplot Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of plotting with Axes vs pyplot
What will be the output of the following code snippet comparing Axes and pyplot interfaces?
Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 5, 6])
plt.plot([1, 2, 3], [6, 5, 4])
print(len(fig.axes))
A2
B3
C0
D1
Attempts:
2 left
💡 Hint
Think about how many axes objects are created and used in the figure.
Predict Output
intermediate
2:00remaining
Number of lines plotted on Axes
After running this code, how many lines are plotted on the Axes object ax?
Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 5, 6])
plt.plot([1, 2, 3], [6, 5, 4])
A1
B3
C2
D0
Attempts:
2 left
💡 Hint
Consider which axes the pyplot plot adds lines to.
🔧 Debug
advanced
2:00remaining
Identify the error in Axes vs pyplot plotting
What error will this code produce?
Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
plt.plot([1, 2, 3], [4, 5, 6])
ax.plot([1, 2, 3], [6, 5, 4])
plt.show()
print(len(fig.axes))
ANo error, output is 1
BNo error, output is 2
CAttributeError: 'NoneType' object has no attribute 'axes'
DTypeError: 'int' object is not callable
Attempts:
2 left
💡 Hint
Check if plt.plot() creates new axes or uses existing ones.
data_output
advanced
2:00remaining
Axes object properties after plotting
After running this code, what is the value of ax.get_lines()[1].get_ydata()?
Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 5, 6])
plt.plot([1, 2, 3], [6, 5, 4])
A[6 5 4]
B[4 5 6]
C[1 2 3]
DIndexError: list index out of range
Attempts:
2 left
💡 Hint
Remember plt.plot() adds to the current Axes lines list.
visualization
expert
3:00remaining
Visual difference between Axes and pyplot interface
Which option best describes the visual output difference when plotting with Axes.plot() vs pyplot.plot() in this code?
Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 5, 6], label='Axes plot')
plt.plot([1, 2, 3], [6, 5, 4], label='Pyplot plot')
ax.legend()
plt.show()
AOne plot with only the 'Axes plot' line visible
BOne plot with two lines and a legend showing both labels
CTwo separate plots appear in different windows
DOne plot with only the 'Pyplot plot' line visible
Attempts:
2 left
💡 Hint
Consider how legend and lines are managed on the same Axes.