How to Use Colormaps in MATLAB
Learn how to utilize colormaps in MATLAB® to more effectively visualize and convey 2D and 3D graphics objects. This video will dive into what colormaps consist of, how to create and customize colormaps, and how to compute with colormaps.
For further learning on colormaps, check out this example code of how to create shared colorbars between multiple colormaps set in a tiled layout in the same figure: how to add one colorbar for the whole figure with tiled layout?
Published: 1 Mar 2021
Hello, and welcome back to another MATLAB video. And today we're talking about color maps. Let's say you have a 2D or 3D graphics object and you want to share the significance of that object. MATLAB color maps lets you quickly and colorfully visualize your objects to make communicating your data easier.
So what is a color map? A color map is a matrix of values that defines the colors for graphics objects, such as a surface, like the classic MATLAB membrane. The object, like membrane here, is drawn through mapping data values to colors in the color map.
Color maps can be any length, but must be three columns wide because it is color data and consists of RGB triplets. So each row is a vector with each element corresponding to the intensities of red, green, and blue, respectively. One end of the created color spectrum is the highest value in the color map, and the other end of the spectrum is the lowest value in the color map.
Now that we have a basic understanding of what goes into making a color map, let's dive into some ways to manipulate an existing color map. One important addition that enhances any color map is the color bar function. By adding a color bar to a color map, you are essentially adding a key for the viewer, which illustrates the range of values in the color map and how they map to their corresponding colors, like adding a color bar to our membrane here shows the entire range of colors represented in the graph.
Another very useful way to manipulate a color map is to CLim function. By using CLim, you can either observe the color maps existing minimum and maximum values shown here, or set them yourself, which allows you to mold your color maps to do things like focus on the important data or cut out outliers. We will also be discussing a different avenue do you see limb later in this video.
All these bright colors are just a bit too much for you, or if you just need to represent your data in black and white, use the bone function to turn any color map into a gray scale range, going from a black minimum to a white maximum. Plus if you have a 3D graph and you would rather represent it in 2D, use the contour f function to switch the 3D graph to a filled-in 2D contour plot. Our membrane is now represented in 2D here.
So far we've discussed how to make and manipulate a color map. So now let's talk about actually doing some computations with color maps. One of the most essential ways to compute with an existing continuous color map is to apply a color map type, like turbo, onto it.
Turbo not only changes the range of colors featured in the color map to be a perceptually uniform rainbow spectrum, but it also returns a matrix of the color map data, which defaults to 256 rows and 3 columns. Creating an array of the color map allows for actual editing of the data contained within the color map.
Turbo is just one of many color map types that can be applied to a color map. And we'll go over the others later in this video. Now that we've created an array of values from a color map, we can also create a color map from an array of values using the colormap function.
Applying the colormap function to one of our arrays here will create the same color map that the array was made from. You can also map discrete data into an existing color map to the colormap function shown here, allowing for visualization of discrete data in addition to continuous data using color maps.
In addition to editing color maps through applying functions to them, you can also use the color map editor for color map customization. By applying the color map editor function to our existing color map, this opens a color map editor window. From within this pane, you can interactively apply various edits to your color map.
As mentioned before, in the select color map section, you can apply turbo as well as a variety of other functions. In the customized color map section, under shift, you can edit the range of colors featured by clicking and dragging. And under specified color, you can exactly point out which colors go where in that color range.
If you want greater contrast in your data visualization, in the edit size and color space section, you can use labels to change the coarseness of the color map from the default 256 levels. In the set color map limits section, just like what the CLim function, you can interactively set the maximum and minimum for the color map.
As you can see, the color map editor combines many functionalities described in this video in an interactive interface for you to be able to control your color maps all in one place. In this video, we have covered the ins and outs of MATLAB color maps, from creation to customizations.
Now that we have a deeper understanding of how using color maps can better visualize 2D and 3D graphics objects, you can get out there and be vibrant and colorful in your data communication. Thanks for watching, and I'll see you guys in another video.