Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add a legend to the plot.
Matplotlib
import matplotlib.pyplot as plt plt.plot([1, 2, 3], [4, 5, 6], label='Line 1') plt.plot([1, 2, 3], [6, 5, 4], label='Line 2') plt.[1]() # Add legend plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using colorbar() instead of legend()
Forgetting to add labels to the plot lines
Trying to use title() to show labels
✗ Incorrect
The legend() function adds a legend to the plot, which helps identify different lines.
2fill in blank
mediumComplete the code to add a colorbar to the scatter plot.
Matplotlib
import matplotlib.pyplot as plt import numpy as np x = np.random.rand(50) y = np.random.rand(50) colors = np.random.rand(50) scatter = plt.scatter(x, y, c=colors) plt.[1](scatter) # Add colorbar plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using legend() instead of colorbar()
Not passing the scatter plot object to the colorbar() function
Trying to add xlabel or ylabel instead
✗ Incorrect
The colorbar() function adds a color scale next to the plot, showing what colors represent.
3fill in blank
hardFix the error in the code to correctly add a legend with a title.
Matplotlib
import matplotlib.pyplot as plt plt.plot([1, 2, 3], [3, 2, 1], label='Data') plt.legend(title=[1]) plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the title text
Using a variable name without defining it
Using underscores instead of spaces without quotes
✗ Incorrect
The title parameter needs a string, so it must be in quotes like "Legend Title".
4fill in blank
hardFill both blanks to create a scatter plot with a colorbar and label the colorbar.
Matplotlib
import matplotlib.pyplot as plt import numpy as np x = np.arange(10) y = np.random.rand(10) colors = np.linspace(0, 1, 10) scatter = plt.scatter(x, y, c=colors, cmap='viridis') cbar = plt.[1](scatter) cbar.set_[2]('Intensity') plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using legend() instead of colorbar()
Using set_title() instead of set_label() for the colorbar
Not assigning the colorbar to a variable before labeling
✗ Incorrect
Use colorbar() to add the color scale, then set_label() to name it.
5fill in blank
hardFill all three blanks to create a line plot with two lines, add a legend, and set the legend location.
Matplotlib
import matplotlib.pyplot as plt x = [1, 2, 3, 4] y1 = [10, 20, 25, 30] y2 = [30, 23, 15, 10] plt.plot(x, y1, label=[1]) plt.plot(x, y2, label=[2]) plt.[3](loc='upper left') plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around labels
Using title() instead of legend()
Forgetting to add labels to the plot lines
✗ Incorrect
Labels must be strings in quotes; legend() adds the legend with location set.