Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a figure and axes using matplotlib.
Matplotlib
import matplotlib.pyplot as plt fig, ax = plt.[1]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.figure() only creates a figure without axes.
Using plt.plot() does not create figure and axes objects.
✗ Incorrect
The plt.subplots() function creates a figure and axes together.
2fill in blank
mediumComplete the code to plot data using the axes-level method.
Matplotlib
fig, ax = plt.subplots() ax.[1]([1, 2, 3], [4, 5, 6]) plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling plt.plot() instead of ax.plot() when using axes-level plotting.
Using ax.show() which does not exist.
✗ Incorrect
The ax.plot() method plots data on the specific axes.
3fill in blank
hardFix the error in the code by choosing the correct figure-level method to set the title.
Matplotlib
fig, ax = plt.subplots() ax.plot([1, 2, 3], [4, 5, 6]) fig.[1]('My Plot Title') plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ax.set_title() to set a figure title instead of a subplot title.
Using fig.title() which does not exist.
✗ Incorrect
The figure-level method to set a title for the whole figure is fig.suptitle().
4fill in blank
hardFill both blanks to set the x and y labels using axes-level methods.
Matplotlib
fig, ax = plt.subplots() ax.plot([1, 2, 3], [4, 5, 6]) ax.[1]('X Label') ax.[2]('Y Label') plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
plt.xlabel() and plt.ylabel(), which are pyplot functions.Using
ax.xlabel(), which does not exist.✗ Incorrect
The axes-level methods are ax.set_xlabel() and ax.set_ylabel().
5fill in blank
hardFill all three blanks to set titles on two subplots using axes-level methods and a main title using figure-level method.
Matplotlib
fig, (ax1, ax2) = plt.subplots(1, 2) ax1.plot([1,2,3], [1,2,3]) ax1.[1]('Left Plot') ax2.plot([3,2,1], [1,2,3]) ax2.[2]('Right Plot') fig.[3]('Comparison') plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
title() without set_ on axes.Using
suptitle() on individual axes instead of figure.✗ Incorrect
Use set_title() on each ax for subplot titles and suptitle() on fig for the main title.