0
0
Matplotlibdata~10 mins

Why legends and colorbars guide reading in Matplotlib - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Alegend
Bcolorbar
Ctitle
Dxlabel
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
2fill in blank
medium

Complete 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'
Aylabel
Blegend
Cxlabel
Dcolorbar
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
3fill in blank
hard

Fix 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'
A'Legend Title'
BLegend Title
C"Legend Title"
DLegend_Title
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
4fill in blank
hard

Fill 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'
Acolorbar
Blegend
Clabel
Dtitle
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
5fill in blank
hard

Fill 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'
A'First Line'
B'Second Line'
Clegend
Dtitle
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around labels
Using title() instead of legend()
Forgetting to add labels to the plot lines