Challenge - 5 Problems
Axes vs Pyplot Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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))
Attempts:
2 left
💡 Hint
Think about how many axes objects are created and used in the figure.
✗ Incorrect
The code creates one figure and one Axes object with plt.subplots(). The ax.plot() adds a line to that Axes. The plt.plot() call without specifying axes adds a new line to the current axes, which is the same Axes. So only one Axes exists in the figure, but two lines are plotted on it. The length of fig.axes is 1, but the question asks for output of print(len(fig.axes)) which is 1. However, plt.plot() outside of ax adds to the current axes, so no new axes are created. So the output is 1.
❓ Predict Output
intermediate2: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])
Attempts:
2 left
💡 Hint
Consider which axes the pyplot plot adds lines to.
✗ Incorrect
Both ax.plot() and plt.plot() add lines to the same Axes object ax because plt.plot() uses the current axes, which is ax here. So ax has two lines plotted.
🔧 Debug
advanced2: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))
Attempts:
2 left
💡 Hint
Check if plt.plot() creates new axes or uses existing ones.
✗ Incorrect
plt.plot() uses the current axes, which is ax created by plt.subplots(). So no new axes are created. fig.axes contains one Axes object, so len(fig.axes) is 1. No error occurs.
❓ data_output
advanced2: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])
Attempts:
2 left
💡 Hint
Remember plt.plot() adds to the current Axes lines list.
✗ Incorrect
ax.get_lines() returns a list of Line2D objects. The first line plotted is with y data [4,5,6], the second line is from plt.plot() with y data [6,5,4]. So ax.get_lines()[1].get_ydata() returns [6 5 4].
❓ visualization
expert3: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()
Attempts:
2 left
💡 Hint
Consider how legend and lines are managed on the same Axes.
✗ Incorrect
Both lines are plotted on the same Axes object ax. The legend shows both labels because both lines belong to ax. So the plot shows two lines with a legend listing 'Axes plot' and 'Pyplot plot'.