0

Customizable Heat Maps matlab画热度图工具箱

2025.05.28 | 念乡人 | 15次围观

源地址:https://ww2.mathworks.cn/matlabcentral/fileexchange/24253-customizable-heat-maps?focused=6785671&tab=example

This scripts demonstrates the capabilities of the heatmap visualization function.

Copyright The MathWorks, Inc. 2014

  • Load Data for Visualization
  • Simple Heatmap
  • Axes Ticks & Labels
  • Heatmap Text Labels
  • Zoom, Pan & Data Cursors
  • Changing the Colormap
  • Multiple Heatmaps on a Figure
  • Forcing Color Levels
  • Missing Values
  • Colorbar
  • Grid Lines

Import data set to visualize. The data set contains a matrix of electricity price differences between locations in New England.

Generate heatmap only with no labels

Customizable Heat Maps  matlab画热度图工具箱

The heatmap above can be made a lot more useful with labels on the columns and rows. We can do this with two more inputs for x and y labels. The labels can be numeric or cell arrays of strings

Customizable Heat Maps  matlab画热度图工具箱

Labels on the x-axis can be rotated to prevent overlap

Customizable Heat Maps  matlab画热度图工具箱

By default, for larger heatmaps, not all ticks are shown. This can be forced with the ShowAllTicks option.

Customizable Heat Maps  matlab画热度图工具箱

The font size of the ticks can also be controlled with the TickFontSize option. This can help when trying to fit many tick labels on a heatmap.

Customizable Heat Maps  matlab画热度图工具箱

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

Customizable Heat Maps  matlab画热度图工具箱

A completely different matrix of data can be shown as text labels on top of the original heatmap, enabling you to overlay another dataset

Customizable Heat Maps  matlab画热度图工具箱

Properties of text labels include FontSize and TextColorTextColor 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.

Customizable Heat Maps  matlab画热度图工具箱

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.

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.

Customizable Heat Maps  matlab画热度图工具箱 Customizable Heat Maps  matlab画热度图工具箱

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

Customizable Heat Maps  matlab画热度图工具箱

The option ColorLevels lets you increase or decrease the number of distinct colors in the colormap.

Customizable Heat Maps  matlab画热度图工具箱

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.

Customizable Heat Maps  matlab画热度图工具箱 Customizable Heat Maps  matlab画热度图工具箱

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.

Customizable Heat Maps  matlab画热度图工具箱

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.

Customizable Heat Maps  matlab画热度图工具箱 Customizable Heat Maps  matlab画热度图工具箱

These two plots show the same heatmaps with consistent color levels. The mincolorvalue and maxcolorvalue have been set to the minimum and maximum of both matrices.

Customizable Heat Maps  matlab画热度图工具箱 Customizable Heat Maps  matlab画热度图工具箱

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

Customizable Heat Maps  matlab画热度图工具箱

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.

Customizable Heat Maps  matlab画热度图工具箱

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.

Customizable Heat Maps  matlab画热度图工具箱

Grid lines can be added with a GridLines option which is a line specification like ':' for a dotted line

Customizable Heat Maps  matlab画热度图工具箱

版权声明

本文系作者授权念乡人发表,未经许可,不得转载。

标签列表