0
0
Matplotlibdata~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Legend and Colorbar Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this matplotlib legend code?

Look at the code below that plots two lines with a legend. What will the legend show?

Matplotlib
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [4, 5, 6], label='Line A')
plt.plot([1, 2, 3], [6, 5, 4], label='Line B')
plt.legend()
legend_texts = [text.get_text() for text in plt.gca().get_legend().get_texts()]
print(legend_texts)
plt.close()  # Prevent showing plot in test environment
A['Line B', 'Line A']
B['Line A', 'Line B']
C['line a', 'line b']
D[]
Attempts:
2 left
💡 Hint

Check the order of plotting and the labels assigned.

data_output
intermediate
2:00remaining
How many colorbar ticks are shown in this heatmap?

This code creates a heatmap with a colorbar. How many ticks will the colorbar display?

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

data = np.random.rand(5,5)
fig, ax = plt.subplots()
cax = ax.imshow(data, cmap='viridis')
cb = fig.colorbar(cax, ticks=[0, 0.5, 1])
plt.close()
tick_labels = [tick.get_text() for tick in cb.ax.get_yticklabels() if tick.get_text() != '']
print(len(tick_labels))
A3
B5
C0
D1
Attempts:
2 left
💡 Hint

Look at the ticks argument passed to the colorbar.

visualization
advanced
3:00remaining
Which option correctly adds a legend and colorbar to guide reading?

Given a scatter plot with points colored by a value, which code snippet correctly adds both a legend for categories and a colorbar for the color scale?

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(5)
y = np.random.rand(5)
colors = np.linspace(0, 1, 5)
categories = ['A', 'B', 'A', 'B', 'A']

fig, ax = plt.subplots()
scatter = ax.scatter(x, y, c=colors, cmap='plasma')
Aax.legend(categories)\nfig.colorbar(scatter)
Bax.scatter(x, y, label=categories)\nfig.colorbar(scatter)
Cfor cat in set(categories):\n ax.scatter([], [], label=cat)\nax.legend()\nfig.colorbar(scatter)
Dax.legend()\nfig.colorbar()
Attempts:
2 left
💡 Hint

Think about how to create legend entries for categories without plotting extra points.

🔧 Debug
advanced
2:00remaining
What error does this code raise when adding a colorbar?

Consider this code snippet that tries to add a colorbar but raises an error. What is the error?

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
scatter = ax.scatter([1,2,3], [4,5,6])
fig.colorbar()
plt.close()
AAttributeError: 'NoneType' object has no attribute 'colorbar'
BNo error, colorbar added successfully
CValueError: Invalid colorbar parameters
DTypeError: colorbar() missing 1 required positional argument: 'mappable'
Attempts:
2 left
💡 Hint

Check the required arguments for fig.colorbar()

🚀 Application
expert
2:30remaining
How does adding legends and colorbars improve data reading in plots?

Which statement best explains why legends and colorbars help people understand plots better?

AThey provide labels and scales that connect colors and symbols to data meaning, making interpretation easier.
BThey decorate the plot with colors and shapes to make it look more attractive.
CThey automatically fix errors in the data visualization code.
DThey reduce the amount of data shown so the plot is less cluttered.
Attempts:
2 left
💡 Hint

Think about how a reader knows what colors or symbols represent.