Heatmap Text Labels

The heatmap image can be overlaid with text strings to either make the heatmap more descriptive or overlay another data set. The text labels can either be just turned on, turned on with a specific format or specified as another numeric matrix or cell array of strings

Turn on text labels with a format $xx.xx. See the documentation of sprintf for more information on format strings

Properties of text labels include FontSize and TextColor. TextColor can also be specified as a string 'xor' in which case a color will automatically be chosen for each label to contrast with the color on the image.

Zoom, Pan & Data Cursors

Heatmaps generated with heatmap are interactive, in that you can zoom and pan to explore the visualization. The tick labels will automatically update in response to zoom and pan events. Data cursors are also supported. The text shown in the data cursor is derived from the text labels used to display the data on the heatmap image.

Changing the Colormap

Heatmaps, by default, use the colormap of the figure in which they are created. Therefore, changing the figure colormap will change that of the heatmap.

clf
heatmap(spreads);
snapnow
colormap cool
snapnow

heatmap includes two custom colormaps. The colormap money displays values of 0 as white and positive and negative values as shades of green or red. The colormap red displays values of 0 as white and positive values as different shades of red. You can also use your own colormaps with the Colormap option

The colormap can also be constructed on a log-scale. This can be useful if your matrix values are not evenly distributed. Using a log-scale colormap will highlight the variation in the small values in your dataset.

Multiple Heatmaps on a Figure

The heatmap function can be used with multiple axes in a figure, such as with the subplot command. By default they will share the colormap of the figure. The option UseFigureColormap, if set to 0 or false, will make each heatmap use a different colormap.

Forcing Color Levels

A common use of heatmaps is to compare two matrices or tables using color to identify a change or difference. By default heatmap adjusts the colormap so that the minimum and maximum data values in the matrix map to the lowest and highest color. This is usually undesirable when comparing heatmaps where a consistent data-to-color mapping is needed. To aid in this effort, heatmap provides two inputs, MinColorValue and MaxColorValue, that control the minimum and maximum values of the data that map to the lowest and highest color levels. Here we use these to ensure comparable colors between two heatmaps.

The following two plots are heatmaps of two matrices with default color levels. Note that the colors are not consistent between them.

Missing Values

NaNs in the matrix are allowed but by default these get mapped to the lowest color value. The option NaNColor enables you to explicitly specify the color that NaN or missing values should take. In the following heatmap, they are set to black to distinguish them from the other elements that contain low values

Colorbar

As seen above, a colorbar can be added to the figure using the Colorbar option. The labels for the colorbar will automatically be formatted using the formats for the text labels on the heatmap image. Notice the $ signs on the colorbar in the image below.

The value for the colorbar option can either be a simple true or false in which case the default colorbar will be drawn, or it can include a cell array of property-value pairs for the colorbar command.