📖 Scenario: You are working on a data visualization project where you want to show different categories with distinct colors. Instead of a smooth gradient, you want a colorbar with clear, separate color blocks.
🎯 Goal: Create a discrete colorbar using matplotlib that shows exactly 4 distinct colors representing 4 categories.
📋 What You'll Learn
Create a list called colors with exactly 4 color names or hex codes.
Create a ListedColormap from the colors list and assign it to cmap.
Create a BoundaryNorm with boundaries [0, 1, 2, 3, 4] and assign it to norm.
Use matplotlib.pyplot to create a figure and axis.
Add a colorbar to the axis using matplotlib.colorbar.ColorbarBase with the cmap and norm.
Display the plot.
💡 Why This Matters
🌍 Real World
Discrete colorbars are useful when you want to show categories or groups clearly in data visualizations, such as different risk levels, types of land use, or survey responses.
💼 Career
Data scientists and analysts often need to create clear and understandable visualizations. Knowing how to make discrete colorbars helps communicate categorical data effectively.
Progress0 / 4 steps
1
Create a list of 4 colors
Create a list called colors with these exact colors: 'red', 'green', 'blue', and 'yellow'.
Matplotlib
Hint
Use square brackets and separate colors with commas.
2
Create a ListedColormap and BoundaryNorm
Import ListedColormap and BoundaryNorm from matplotlib.colors. Then create a ListedColormap called cmap from the colors list. Also create a BoundaryNorm called norm with boundaries [0, 1, 2, 3, 4] and number of colors equal to the length of colors.
Matplotlib
Hint
Use ListedColormap(colors) and BoundaryNorm([0, 1, 2, 3, 4], ncolors=len(colors)).
3
Create figure and axis, add discrete colorbar
Import matplotlib.pyplot as plt. Create a figure and axis using plt.subplots(). Then import ColorbarBase from matplotlib.colorbar and create a colorbar on the axis using ColorbarBase with the cmap and norm. Use orientation='vertical' for the colorbar.
Matplotlib
Hint
Use plt.subplots() and ColorbarBase with the correct arguments.
4
Display the plot
Use plt.show() to display the figure with the discrete colorbar.
Matplotlib
Hint
Use plt.show() to see the colorbar.
Practice
(1/5)
1. What is the main purpose of using a discrete colorbar in a matplotlib plot?
easy
A. To increase the size of the plot figure
B. To create a continuous gradient of colors for smooth data
C. To add labels to the x-axis of a plot
D. To show data with distinct color steps for clear category separation
Solution
Step 1: Understand colorbar types
Continuous colorbars show smooth color changes, while discrete colorbars use fixed color steps.
Step 2: Identify the purpose of discrete colorbars
Discrete colorbars help separate data into clear categories using distinct colors.
Final Answer:
To show data with distinct color steps for clear category separation -> Option D
Quick Check:
Discrete colorbar = distinct color steps [OK]
Hint: Discrete colorbars separate categories with fixed colors [OK]
Common Mistakes:
Confusing discrete with continuous colorbars
Thinking colorbars change plot size
Mixing colorbar purpose with axis labeling
2. Which of the following code snippets correctly creates a discrete colorbar with 3 color steps using matplotlib?
easy
A. cmap = ListedColormap(['red', 'green', 'blue'])
norm = Normalize(0, 3)
B. cmap = LinearSegmentedColormap(['red', 'green', 'blue'])
norm = Normalize(0, 3)
5. You have data with 5 categories labeled 0 to 4. You want a discrete colorbar with distinct colors for each category and labels shown on the colorbar ticks. Which approach correctly achieves this in matplotlib?
hard
A. Use ListedColormap with 5 colors, BoundaryNorm with boundaries [0,1,2,3,4,5], and set colorbar ticks at [0.5,1.5,2.5,3.5,4.5] with labels ['0','1','2','3','4']
B. Use LinearSegmentedColormap with 5 colors and Normalize from 0 to 4, then set colorbar ticks at [0,1,2,3,4]
C. Use ListedColormap with 4 colors, BoundaryNorm with boundaries [0,1,2,3,4], and set colorbar ticks at [0,1,2,3]
D. Use ListedColormap with 5 colors, Normalize from 0 to 5, and set colorbar ticks at [0,1,2,3,4]
Solution
Step 1: Set colormap and boundaries for 5 categories
ListedColormap with 5 colors matches 5 categories; BoundaryNorm boundaries must be length 6 for 5 intervals.
Step 2: Position ticks between boundaries
Ticks at midpoints like 0.5,1.5,... label categories clearly on discrete colorbar.
Final Answer:
Use ListedColormap with 5 colors, BoundaryNorm with boundaries [0,1,2,3,4,5], and set colorbar ticks at [0.5,1.5,2.5,3.5,4.5] with labels ['0','1','2','3','4'] -> Option A