MATLAB Colormap Tutorial (Images): Heatmap, Lines and Examples
Table of Contents (click to navigate)
What is MATLAB Colormap?
Colormap is a three color matrix that specifies the red, green and blue content for graphics objects such as surface, image, and patch objects. The RGB values are triplets which means that they’re in the order of Red, Green and Blue and they can range from 0 to 1. MATLAB draws the objects by mapping data values to colors in the colormap.
A colormap can have upto 255 rows which shows how many
values can be stored in a single byte. RBG triplet vector shows the intensities
of the three basic color components. A value
0 indicates no color and a value of
1 indicates full
Colormap map sets the colormap for the current figure to one of the predefined colormaps. If you set the colormap for the figure, then axes and charts in the figure use the same colormap. The new colormap is the same length (number of colors) as the current colormap. When you use this syntax, you cannot specify a custom length for the colormap.
For example: Table 1.
abdul = [0 0 0, %black 0 0 1, %red 0 1 0, %green 1 0 0] %blue % Define a square matrix SqMat = zeros(15,15); SqMat(7:9,7:9) = 1; SqMat(7:9,3:5) = 2; SqMat(7:9,11:13) = 3; %Display figure(1) colormap (abdul) imagesc (SqMat)
The following table lists the predefined colormaps.
Create a Custom Colormap
create a custom colormap, you have to specify
map as a three-column matrix of RGB triplets where
each row defines one color. The intensities must be in the range
from 0 to1.
- Create a custom colormap by defining a three-column matrix of values between 0.0 and 1.0. Each row defines a three-element RGB triplet vector value.
- First column specifies the red intensities.
- Second column specifies the green intensities.
- Third column specifies the blue intensities. . For example, this matrix defines a colormap containing five colors.
map = [0.2 0.1 0.5 0.1 0.5 0.8 0.2 0.7 0.6 0.8 0.7 0.3 0.9 1 0]; surf(peaks) colormap(map)
Go to the custom colormap settings encircled in the figure 2(b). You’ll see a window:
How to Return Colormap Values Used in Plot
order to return the values of colormap, ‘cmap’ command is used.Try the above
example along with the following code. First create a surface plot of the
peaks function and specify a colormap.
map = [0.2 0.1 0.5 0.1 0.5 0.8 0.2 0.7 0.6 0.8 0.7 0.3 0.9 1 0]; surf(peaks) colormap(map) mesh(peaks); colormap(autumn(5)) cmap = colormap
Command window output:
How to Return Colormap Values for Specific Axis
- First step is to return the colormap values for a specific axes by passing its axes handle to the
- Now create a figure with two subplots and return the axes handles,
x2.Use a different colormap for each axis and add a filled contour for each axis.
- Return the colormap values used in the upper subplot by passing its axes handle,
x1, to the
- The figure colormap affects all the axes in plots within the figure.
- Define a unique colormap for the different axes within a figure.
- Graphics object that has a
Colormapproperty. For example, you can change the colormap for a
HeatmapChartobject fig. 4(a).
- Each row of the matrix defines one RGB triplet specifying one color of the colormap. The values are in the range from 0 to 1. Colormap values will return as a three-column matrix of RGB triplets.
x1 = subplot(2,1,1); contourf(peaks) colormap(x1,hot(8)) x2 = subplot(2,1,2); contourf(peaks) colormap(x2,pink) cmap = colormap(x1)
Change the Colormap of Imported Images
- Import & read image:
- Change the search directory and folder from the option shown in figure 5(a).
- Select the image you want to import.
- The name of image will show in workplace.
- Enter the command “imread(image name.image format)” in command prompt.
- For example: imread(xxx.PNG).
- Analysing RGB component using Data Cursor:
- The selected image will open.
- Click the data cursor button highlighted in fig. 5(c).
- Click on the image and a window will open showing RGB triplet vector values shown in fig. 5(d).
- Move the cursor on the area for which you want to know the value.
Change the Colormap of Your Image:
Select the custom colormap option shown in fig. 2.(a).
Select the colormap of your choice or you can customize it shown in figure 5.(e).
Controlling the Limits of Colormap
Many types of combinations you create, it is the feature of MATLAB to map the full range of your data to the colormap. Smallest value in the data maps to the first row in the colormap and the largest value maps to the last row in the colormap. All middle values map linearly to the middle rows of the colormap.
Even if the range you choose is different than the range of your data, it is possible to perform the mapping over for any range you choose. Default mapping is mostly used in different cases .Choosing a different mapping range allows you:
- To see wether your data is at the limit or beyond the limits of that range.
- To see if your data lies within that range.
Take an example, consider the surface Z = X + Y and the limit condition is:
[–30 ≤ x ≤ 30 and –30 ≤ y ≤ 30].
[X,Y] = meshgrid(-30:30); Z = X + Y; surf(X,Y,Z); xlabel('X'); ylabel('Y'); zlabel('Z = C'); % C defines ‘Clim’ colorbar
How Surface Plot Data Relates to a Colormap
surface is defined by the Z-coordinates of points above a grid in the x–y plane. Surface plot data describes
the properties that control the color in this presentation. The ‘
CData’ property of the
surface object contains an
C’ that is
associated to each grid point (on the surface) to a color in the colormap. By
Z is the array that contains the
values of z = f(x,y) at the
grid points. Hence, the colors vary when Z changes.
[cindx,cimap,clim] = shaderel(X,Y,Z,cmap) generates the colormap and color indices
which allows a surface to be displayed in colored shaded relief. The colors are
proportional to the magnitude of
Z but lie in the shades of gray based on the surface normals
to simulate surface lighting. This representation allows both large and
small-scale differences to be seen.
Z define the surface.
CLim’ property for
surface plots, patches, graphics object or any images that use a colormap can
be customized. The Z values determine a
surface of X and Y grid lines and fill color from a color map. However, this property has only
affect on graphic objects that have the ‘
CData mapping property’ set
'scaled'. If it is set to
'direct', then all values
C’ index will direct into the
colormap without any scaling.
- Values of ‘
C’that are less than 1 are clipped and show as the first color in the colormap.
- Values of ‘
C’greater than the length of the colormap are clipped and are shown as the last color in the colormap.
Make sure your clipping is set to ‘ON’.
‘Colormap’ command in MATLAB software can be used for the following purposes:
- It has the function of color scheme editing of an image, figure,plots, mesh etc. We can isolate a single color from any image while all other colors are set to 0.
- We can find RBG vector value at any point on a particular image. This is helpful in image processing.
- Wecan make our own filter effects to create a beautiful image.
- MATLAB Software
Abdul Mannan is an Electrical Power Engineer with specialization in High Voltage. He’s the founder and former president at Youth Entrepreneurship Society (YES), University of Engineering & Technology Taxila Campus. He is the leading contributor at “Right to Write”. Connect with him about Entrepreneurship, startup ideas, creative writing, business strategies via linked in.