February 17

MATLAB Colormap Tutorial (Images): Heatmap, Lines and Examples

MATLAB Colormap Tutorial (Images): Heatmap, Lines and Examples

Abdul Mannan

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 of 0 indicates no color and a value of 1 indicates full intensity.

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;
colormap (abdul)
imagesc (SqMat)

Fig. 1(a)

Colormap Name

The following table lists the predefined colormaps.

Table 2.

Create a Custom Colormap

To 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.

Matlab code:

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]; 

Fig. 2(a)

Go to the custom colormap settings encircled in the figure 2(b). You’ll see a window:

Fig. 2(b)

How to Return Colormap Values Used in Plot

In 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.

MATLAB code:

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];
cmap = colormap                                                                                    

Command window output:

Fig. 3(a)     

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 colormap function.
  • Now create a figure with two subplots and return the axes handles, x1 and 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 colormap function.       

Fig. 4.(a)    

  • 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 Colormap property. For example, you can change the colormap for a HeatmapChart object 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.

MATLAB code:

x1 = subplot(2,1,1);
x2 = subplot(2,1,2);
cmap = colormap(x1)

Fig. 4(b)

Change the Colormap of Imported Images

  1. Import & read image:
  2. Change the search directory and folder from the option shown in figure 5(a).
  3. Select the image you want to import.
  4. The name of image will show in workplace.
  5. Enter the command “imread(image name.image format)” in command prompt.
  6. For example: imread(xxx.PNG).                                                                                 


Fig. 5(b)

  • 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.

 Fig. 5(c) 

Fig. 5(d)

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).

Fig. 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;
zlabel('Z = C'); % C defines ‘Clim’

Fig. 6(a)

How Surface Plot Data Relates to a Colormap

In MATLAB, surface is defined by the Z-coordinates of points above a grid in the xy plane. Surface plot data describes the properties that control the color in this presentation. The ‘CData’ property of the surface object contains an array ‘C’ that is associated to each grid point (on the surface) to a color in the colormap. By default, Z = C where 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. XY, and 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 to 'scaled'. If it is set to 'direct', then all values of ‘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’.

Fig. 9(a)


‘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.


Loved this? Spread the word

About the Author

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.

Abdul Mannan

Related posts

10+ Best Construction Takeoff Software & Tools for Contractors in 2022

​Read More

Compare Autodesk Inventor vs Fusion 360: Review Which One to Buy

​Read More

How to Use Convert Entities Tool in SolidWorks: Beginner Tutorial

​Read More

[May 2020] Best Free CAD Design Software for Mac

​Read More
{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Subscribe to our newsletter now!