Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a figure with two subplots side by side.
Matplotlib
import matplotlib.pyplot as plt fig, axs = plt.subplots(1, [1]) plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 2 will create only one plot.
Confusing rows and columns.
✗ Incorrect
The code creates a figure with 1 row and 2 columns of subplots, so 2 subplots side by side.
2fill in blank
mediumComplete the code to plot data on the first subplot.
Matplotlib
import matplotlib.pyplot as plt fig, axs = plt.subplots(1, 2) axs[[1]].plot([1, 2, 3], [4, 5, 6]) plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 causes an error or plots on the second subplot.
Using 2 causes an index error.
✗ Incorrect
Subplots are indexed starting at 0, so the first subplot is axs[0].
3fill in blank
hardFix the error in the code to plot on both subplots.
Matplotlib
import matplotlib.pyplot as plt fig, axs = plt.subplots(1, 2) [1].plot([1, 2, 3], [4, 5, 6]) axs[1].plot([1, 2, 3], [6, 5, 4]) plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call plot directly on axs causes an error.
Confusing fig and axs.
✗ Incorrect
axs is an array of subplots, so to plot on the first subplot, use axs[0].
4fill in blank
hardFill both blanks to create a figure with 2 rows and 1 column and plot on the second subplot.
Matplotlib
import matplotlib.pyplot as plt fig, axs = plt.subplots([1], [2]) axs[1].plot([10, 20, 30], [3, 2, 1]) plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping rows and columns.
Using 1 row and 2 columns instead.
✗ Incorrect
The figure has 2 rows and 1 column, so plt.subplots(2, 1). The second subplot is axs[1].
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps each subplot index to the number of points plotted if the points are more than 2.
Matplotlib
points = [1, 2, 3, 4] result = {i: len(points) for i in range([1]) if len(points) [2] [3]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using range(3) excludes the last index.
Using '<' instead of '>' changes the condition.
Using 3 instead of 2 in the condition.
✗ Incorrect
The comprehension iterates over 4 indices (0 to 3). It includes only if len(points) > 2, which is true since len(points) is 4.