Matlab

How to Change Color Scheme Using a Colormap in MATLAB

MATLAB uses a default color scheme to display visualizations like surface plots. This color scheme can be changed by specifying the colormap. Colormaps are typically three-column arrays of RGB triplets where each row represents a different color. This article will teach us how to change a color scheme in MATLAB using colormap using some practical examples.

What is the MATLAB Colormap?

Different colors are used to create the plots. The color variation enhances the plots’ three-dimensional visualization. The colormap(C) command can be utilized for changing the color. In this command, C represents a three-element vector whose first, second, and third elements, respectively, determine the intensity of the RGB colors (Red, Green, and Blue). Each element of the vector C can be a number ranging from 0 (lowest intensity) to 1 (highest intensity). Some of these colors are listed below:

  • C = [1 0 0] represents red
  • C = [1 1 0] represents yellow
  • C = [1 0 1] represents magenta
  • C = [0 0 1] represents blue
  • C = [0 0 0] represents black
  • C = [0.5 0.5 0.5] represents gray
  • C = [0 1 0] represents green

Now consider some examples which demonstrate the usage of colormap in MATLAB.

Example 1

In the given example, a default colormap is used in the plot visualization.

surf(peaks)
colormap default

Example 2

This MATLAB code generates a surface plot and defines the colormap winter.

surf(peaks)
colormap winter

Example 3

In this example, we are working with two axes, so we assign a different colormap to each axis as shown below.

tiledlayout(2,2);
ax1 = nexttile;
surf(peaks);
shading interp;
colormap(ax1,parula(5));
ax2 = nexttile;
surf(peaks);
shading interp;
colormap(ax2,cool(5));

Example 4

In this example first, we define a matrix in which each row defines a different color using the RGB triplet. These colors are red, blue, black, white, and green respectively. After that, we make a surface and use the colormap function to change the visualization’s color scheme.

cmap = [1 0 0; 0 0 1; 0 0 0; 1 1 1; 0 1 0];
surf(peaks)
colormap(cmap)

Conclusion

Surface plots and other visualizations are displayed by MATLAB using a default color scheme. The colormap can be used to modify this color scheme. In most cases, colormaps are three-column RGB triplet arrays where each row denotes a different color. This tutorial implemented the colormap function using multiple ways as illustrated in the examples.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.