MATLAB or Matrix Laboratory is a programming language that was developed by MathWorks. This powerful language finds its utility in technical computing. MATLAB provides us with a convenient environment that can be used to integrate tasks like manipulations on matrix, plotting data and functions, implementing algorithms, creating user interfaces, etc. MATLAB is also convenient as it gives the solutions in the form that its user can easily understand. It uses mathematical notations to display the solutions. In this topic, we are going to learn about Colormap in Matlab.
Below are a few areas where we can use MATLAB:
- Development of Algorithms
- Analysis and Visualization of data
- Scientific graphs
- Engineering graphics
- Developing applications
MATLAB comprises a number of techniques and functions to perform the above-mentioned capabilities. The objective of this article is to have a thorough understanding of colormaps in MATLAB. As the name suggests, the purpose of colormap is to define the colors of the graphics objects like image, surface and patch objects. A colormap is basically a matrix with values between 0 & 1.
Colormaps can have any length, but width-wise they must have 3 columns. Each row of the matrix defines one color by using an RGB triplet. As you might be aware, the RGB triplet is a 3-element row vector with its elements specifying the intensities of red, green & blue color. As mentioned above, these intensities are in the range [0, 1]. 0 indicates no color & 1 indicates the full intensity of the color.
Below is the list of RGB triplets to get various colors:
|Yellow||[ 1 1 0 ]|
|Magenta||[ 1 0 1 ]|
|Cyan||[ 0 1 1 ]|
|Red||[ 1 0 0 ]|
|Green||[ 0 1 0 ]|
|Blue||[ 0 0 1 ]|
|White||[ 1 1 1 ]|
|Black||[ 0 0 0 ]|
MATLAB also supports some predefined colormaps.
Below is the table showing codes for these predefined colormaps:
Below is the list of SYNTAX used for colormap:
cmap = colormap
cmap = colormap
Examples of Colormap in Matlab
Let us now understand these ones by one with the help of examples:
1. colormap map
This command is used to set the color of the figure to some predefined colormap. While using this command, we cannot specify the length of colormap as per our choice. The axes and charts in the figure will use the same colormap as the predefined colormap selected by us.
Example: We will create a surface plot and will set the colormap to ‘summer’ (which is a pre-defined colormap)
Our commands will look like:
surf(peaks) colormap winter
We can change our output back to the default value by simply using the command:
We can use this command if we want to set the colormap for our current figure to the colormap specified by the argument ‘map’.
So, as per the table above for colormap codes, let’s use ‘parula’ as our colormap and let use 5 as the number of colors that we want to use out of ‘parula’ colormap.
Our command will look like:
3. cmap = colormap
This command returns the colormap for our current figure as a 3-column matrix utilizing RGB triplets. Each row of the matrix will be an RGB triplet color value that will specify one color of the colormap.
Our command will be like:
cmap = colormap
To understand this lets first create a surface plot with some colormap:
mesh(peaks) colormap(autumn (5))
Now if we pass the command:
cmap = colormap;