Legends and colorbars help people understand what different colors or symbols mean in a chart. They make reading graphs easier and clearer.
Why legends and colorbars guide reading in Matplotlib
plt.legend(label_list) plt.colorbar(mappable)
plt.legend() adds a box explaining symbols or colors used in the plot.
plt.colorbar() adds a scale showing how colors map to data values.
plt.plot(x, y1, label='Line 1') plt.plot(x, y2, label='Line 2') plt.legend()
im = plt.imshow(data, cmap='viridis')
plt.colorbar(im)This code draws two lines with a legend to explain them. It also shows a heatmap with a colorbar to explain the colors.
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x) plt.plot(x, y1, label='Sine wave') plt.plot(x, y2, label='Cosine wave') plt.legend() # Create a heatmap np.random.seed(0) data = np.random.rand(10, 10) im = plt.imshow(data, cmap='plasma') plt.colorbar(im) plt.title('Line plot with legend and heatmap with colorbar') plt.show()
Always add legends or colorbars when your plot uses colors or symbols to represent different data.
Legends help identify categories, colorbars help understand continuous values.
Without them, your audience might get confused about what the colors or symbols mean.
Legends explain what different lines or symbols mean in a plot.
Colorbars show how colors relate to data values in heatmaps or similar plots.
Using them makes your charts easier to read and understand.
Practice
Solution
Step 1: Understand the role of a legend
A legend helps explain what different colors or symbols in a plot mean, making the plot easier to read.Step 2: Compare options with legend purpose
Options A, B, and C describe other plot features unrelated to legends.Final Answer:
It explains what different colors or symbols represent in the plot. -> Option AQuick Check:
Legend explains symbols/colors = D [OK]
- Thinking legends change plot colors
- Confusing legends with grid lines
- Assuming legends save files
Solution
Step 1: Recall matplotlib functions for legends
The functionplt.legend()adds a legend to the plot.Step 2: Identify other functions' purposes
plt.colorbar()adds a colorbar,plt.title()adds a title, andplt.grid()adds grid lines.Final Answer:
plt.legend() -> Option DQuick Check:
Legend function = plt.legend() [OK]
- Using plt.colorbar() to add legends
- Confusing plt.title() with legend function
- Trying plt.grid() for legends
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 10, 100) y = np.sin(x) plt.scatter(x, y, c=y, cmap='viridis') plt.colorbar() plt.show()
Solution
Step 1: Analyze the scatter plot code
The scatter plot colors points by their y-values using the 'viridis' colormap.Step 2: Understand the colorbar function
plt.colorbar()adds a colorbar that explains the color scale used for the points.Final Answer:
A scatter plot with points colored by their y-value and a colorbar explaining the colors. -> Option AQuick Check:
Scatter with colorbar explains colors = A [OK]
- Thinking colorbar adds legend labels
- Assuming scatter points all same color
- Believing colorbar causes error with scatter
Solution
Step 1: Identify how to add legends for multiple lines
Each line needs a label, and callingplt.legend()shows these labels as a legend.Step 2: Evaluate other options
plt.colorbar()is for color scales,plt.title()adds a title, and changing background color does not add legends.Final Answer:
Add labels to each plot line and call plt.legend() after plotting. -> Option BQuick Check:
Labels + plt.legend() = legend shown [OK]
- Using plt.colorbar() to add legend
- Relying on plot title for legend info
- Changing background color expecting legend
Solution
Step 1: Understand color mapping in heatmaps
Heatmaps use colors to represent data values, so a colorbar explains what each color means.Step 2: Compare other options
A legend with labels for each value is impractical for continuous data; grid lines and titles help layout but don't explain colors.Final Answer:
Add a colorbar to the heatmap to explain the color scale. -> Option CQuick Check:
Colorbar explains heatmap colors = C [OK]
- Using legend for continuous color scales
- Thinking grid lines explain colors
- Relying on title to explain color meaning
Ready to prove it?
Pass the test and this pattern moves unknown
