Discrete colorbars
📖 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