Heatmap charts display data as an image where each element is represented by a color. The value of the element is also displayed on the image. As a simple example, let’s visualize a magic square.
m = magic(15); heatmap(m);
You can imagine that if the size of the matrix is large, or if the figure is resized to a small window, the image can quickly become crowded with a lot of text. No need to worry. The heatmap intelligently turns off the text display when it makes sense to do so.
set(gcf,'Position',[100 100 400 350])
heatmap returns a HeatmapChart object, which you can use to customize.
figure hMap = heatmap(m); hMap.ColorbarVisible = 'off'; hMap.CellLabelColor = 'red'; hMap.GridVisible = 'off';
Another feature of heatmap that I like is the ability to pass a table data. For tables, you specify the x and the y variables, and the function will calculate and display the aggregated result, such as the count, of the various x-y combinations.
For example, if we have a table of data for 100 people with information about whether they smoke and their self-assessed health status, we can group them into the various categories and visualize as a heatmap.
load patients tbl = table(Age,Gender,SelfAssessedHealthStatus,Smoker,Weight,Location); heatmap(tbl,'Smoker','SelfAssessedHealthStatus');
Instead of showing the aggregated count, we can choose to display the average age for each category.
heatmap(tbl,'Smoker','SelfAssessedHealthStatus', ... 'ColorVariable','Age');
Or the median weight.
heatmap(tbl,'Smoker','SelfAssessedHealthStatus', ... 'ColorVariable','Weight','ColorMethod','median');
For Previous Releases
If you have R2017a, give it a try and let us know what you think here!
Get the MATLAB code
Published with MATLAB® R2017a
3 CommentsOldest to Newest
Just tried it out and i think it could be very helpfull when debugging image processing.
It would be great if it was zoomable and if cell values re-apear when zoomed in.
If you happen to have Image Processing Toolbox, you can use the Image Viewer App. It has an overview tool and an pixel region tool that gives you what you’re looking for.
This should give you an idea: (requires Image Processing Toolbox)
im = imread('cameraman.tif'); h = imshow(im); hp = imscrollpanel(gcf,h); imoverview(h) impixelregion(h)
It’s really a very useful function. Is there any way to change the position of x and y tick marks and labels to be left aligned or right aligned? For instance, in the above example, tick mark for median of weights is in the middle and exactly where it should be. But tick marks and labels for the first one are in the middle and I would like to have it at the edge where grid lines are. It would be beneficial to describe the data range.