0
0
MATLABdata~5 mins

Colormap and colorbar in MATLAB

Choose your learning style9 modes available
Introduction

Colormaps help us show data using colors. Colorbars explain what those colors mean.

When you want to show temperature changes on a map using colors.
When you have a heatmap and want to explain the color scale.
When you want to make your data visualization easier to understand by adding color meaning.
When you want to change the default colors in a plot to better show differences.
When you want to add a legend for colors in a 2D or 3D plot.
Syntax
MATLAB
colormap(name_or_matrix)
colorbar

colormap sets the colors used in the plot.

colorbar adds a bar showing the color scale.

Examples
Use the 'jet' colormap and add a colorbar to the plot.
MATLAB
colormap(jet)
colorbar
Use the 'parula' colormap, which is the default in newer MATLAB versions, and add a colorbar.
MATLAB
colormap(parula)
colorbar
Create a 'hot' colormap with 10 colors, apply it, and show the colorbar.
MATLAB
cmap = hot(10);
colormap(cmap)
colorbar
Sample Program

This code creates a 3D surface plot of the 'peaks' function, applies the 'jet' colormap, and adds a colorbar to explain the colors.

MATLAB
Z = peaks(20);
surf(Z)
colormap(jet)
colorbar
OutputSuccess
Important Notes

You can use built-in colormaps like 'jet', 'parula', 'hot', 'cool', 'spring', etc.

Changing the colormap changes how data values map to colors.

Adding a colorbar helps viewers understand the color meaning in your plot.

Summary

Colormaps assign colors to data values in plots.

Colorbars show the scale of colors used.

Use colormap to set colors and colorbar to add the legend.