File Exchange

image thumbnail

Customizable Heat Maps

version (1.17 MB) by Ameya Deoras
Visualize data as a heatmap with many customizable options.


Updated 01 Sep 2016

View Version History

View License

********** Updated for 2014b *********
HEATMAP displays a matrix as an image whose color intensities reflect the magnitude of its values. In addition, it enables you to specify the following properties:
* X- and Y-axes tick labels:
Display the row/column indices or any other numeric or text labels. X-axis tick labels can even be rotated.
* Text labels:
Overlay the heatmap image with formatted text labels. The text labels can be derived from the original numeric matrix or a different matrix or cell array for displaying another dimension of data. You can control the font size and font color of the labels. The labels update automatically with zooming, panning or resizing the figure.

* Custom color maps:
Use MATLAB's default color maps or specify your own. The function provides two additional color maps - "money" (shown in the example image) and "red" (a color map of red color intensities). Specify Linear or Logarithmic color maps and the number of color levels. You can even use different color maps for different heat maps within a figure.

* Other configurable parameters such as grid lines, color bars.

For detailed examples, see the associated document heatmap_examples.m

NOTE: If using rotated tick labels, HEATMAP will resize the axes to make room for the tick labels. When overwriting existing heatmap plots with a new heatmap, use CLF to first clear the figure. See heatmap_examples for an illustration.

Cite As

Ameya Deoras (2021). Customizable Heat Maps (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (67)

Jacques Leplat

Valdelírio Silva


Sebastien Richoz


Poulomi Ganguli

Hello, if I want to put Y-axes labels at a certain interval (say at an increment of 4 values), how should I get it?


Noah Homolka



Could you indicate me how to change the FontName properties of the heatmap? The heatmap function presented here does not use the default FontName that I set in the startup.m.

Thank you.

Paul Macey

This did everything I wanted for displaying a correlation table - thanks so much for putting in the time to make such a nice tool.

Hello Ameya,
Thanks for the great function.
I am trying to plot a 200-by-200 matrix, and the matrix is the only parameter I pass on to the heatmap() function. According to the examples, it should plot a heatmap without X and Y tick labels. However, it does add tick labels to both axes, and I couldn't find any way to get rid of them. How should it be done?

Chun-Ying Wang

Samuel de Vries

Brad Stiritz

Fantastic work, thank you very much for your effort. This is super-appreciated!

One minor issue : R2017a introduces a native built-in function 'heatmap'. Your function has this same name. I'm sorry to have to ask: would you please rename your function, so that it does not collide in R2017a and later?

Bangqian Chen

That is real what I want, thank you!

Novak Djokovic

Salomon Muller

Notice the row values are flipped [relative to the default HeatMap function]. i.e. row 1 is shown on top rather than at the bottom.

Shay Ben-Sasson


Audrey Cheong

Thanks for posting! Very helpful!

Timothy Purwin

cheng joylin


multi-line titles get cut-off. is there a way to include a multi-line string as a title?



for larger matrices (e.x. 7 x 34), the text does not show up in each cell, even if "textmat" == 1



GREAT function.
Much better than Matlab's implementation.

A couple of problems I noticed:
1) 'UseFigureColorMap' does not extend to Colorbars. Thus, while I can choose to "hard-code" the heatmap's colors, the corresponding colorbar still follows the general figure's colorbar.


Someone help; why can't I view any heatmaps when I run the examples?


something else that just caught my eye: Is there a possibility to easily set the colorbar label when one does NOT use labels in the heat map?

Secondly: It would also be very nice to find a replacement for 'xor' Textcolor.

Third: It would be cool if text labels of 'nan' fields could simply be left out instead of saying 'nan'

Edward Zhao

Alexander Stepanov

There's also a bug in @getGraphicsObjectsPositions. If there's only one column in a heatmap (i.e. one column label), then "extents" is not a cell array. So you need to add the following check in two corresponding places where it is being used as a cell array:

if iscell(extents)
extents = vertcat(extents{:}); % Collect heights in one matrix

Alexander Stepanov

And another small issue: you should not rely on the fact that axes are the current axes. Specifically, all the calls to @colorbar, such as the one below, should specify axes through 'peer' property:

c = colorbar(p.Colorbar{:});
% should be
c = colorbar(p.Colorbar{:}, 'peer', p.hAxes);
% etc

Alexander Stepanov

To clarify, I made the following changes:

% in parseInputs():

% in setAxesTickLabels():
if ~isequal(p.YTick, 'NOT_SET')
ytick = p.YTick;
ytick = get(p.hAxes, 'YTick');
% and the same for XTick

Also, adjustAxesToAccommodateTickLabels() throws an exception when you pass empty hXText to it (that happens if you set 'XTick' to []). So I had to add the following to the beginning of this function:

if isempty(hXText)

Alexander Stepanov

Great utility, thanks!

One thing it still does not allow you to do, is set custom ticks on x/y axis. If there are too many ticks, you cannot use 'ShowAllTicks' option, and the ticks Matlab chooses by default may not be the ones you actually want. Essentially I had to modify the function to add two optional key-value parameters: 'XTick' and 'YTick' (the change is trivial, but would be nice to see it in the official version)

And another minor thing: when using it to plot correlations, you generally want your axes to be square, so I always end up adding the following line after using @heatmap:
set(axh, 'DataAspectRatio', [1 1 1]);
Would be nice if @heatmap accepted a flag and did it automatically if requested


Same comment as arnold. Textcolor 'xor' does not work with R2014b for the 'R2014b graphics ready' version, toolbox nor zip downloads as updated 3 Oct 2014. I note that 'erasemode' 'xor' is still in the code of the zipped package. Otherwise excellent!



The textcolor 'xor' property does not work for me. It looks odd when rendered but when I try to save the figure as pdf, jpg or some other image format, all the text returns to black thus unreadable if the colormap has very dark values in some areas.

Franck Dernoncourt

Great function!

One issue on R2014a :
load heatmapData


Error using hg.axes/set
The name 'TickLabelInterpreter' is not an accessible property for an instance of class 'axes'.


comment out line 331 in heatmap.m:

% set(p.hAxes,'TickLabelInterpreter','none');

Franck Dernoncourt


Thank you very much for this awesome function!
But I'm actually having a problem with the min/max color value of the ColorBar. When I try the following code:

h = heatmap(x, [], [],'%0.4f', 'Colorbar', true, 'MinColorValue', 0.0, 'MaxColorValue', 1.0, 'Colormap', 'autumn', 'Colorlevels', 10, 'Gridlines', '-', 'FontSize', 15, 'UseFigureColormap', false);

It returns an error:
Subscript indices must either be real positive integers or logicals.

Error in heatmap>plotHeatmap (line 235)
p.cdata = reshape(p.Colormap(p.cdata(:),:),[size(p.cdata) 3]);
Error in heatmap (line 128)
p = plotHeatmap(p, mat); % New properties hImage and cdata added

And plots nothing. Am I doing something wrong?

Jong-Hwan Kim

Awesome!! better than built-in HeatMap function in Matlab. Thanks.



nice improvements. I got one big problem with the colorbar:
How do I set a ylabel to the colorbar?

Another idea for NaNs would be to just leave out the text "NaN" as well, simply don't show any text in those fields.

Something else I thought of:
the y-labels of the colorbar are always set automatically (or is there a way I don't see?). One tick is close to the top, as it should be, yet there is no label close to the bottom of the colorbar. This is not nice. They should be evenly spread across the colorbar with one close to the top value and one close to the bottom value. :)

Ameya Deoras

Karin, thanks for catching that oversight. Jonathan, technically "iscellstr" is correct but since the text function allows empty numeric values as the string, I will change it to iscell.

The changes will be in today's update. Thanks!

Patrick Anderson

This is an excellent utility and I appreciate the effort of the creator Ameya Deoras.

I had only minor difficulties accessing the 'Xlabel' and similar attributes. Users should be aware that the "colormap" property is critical for proper visualization (as acknowledged by author).


Thanks Karin for your comment, it works!


great function! very nice options.

small bug: on line 589,

iscellstr(textmat) should be iscell(textmat)


Hi, It is a nice function and precisely what I needed. I also got the same error as Anna, but I think I solved it.

The function 'CreateXTicks' (line 359) needs 5 input arguments. If you look at line 349 these 5 input arguments are used: hXText = createXTicks(p.hAxes, p.TickAngle, xtick, xlab(xtick), p.TickTexInterpreter). However, if you look at line 512 only 4 input argumetents are used: axInfo.hXText = createXTicks(hAxes, p.TickAngle, xticks, xlabels(xticks)). If you put in p.TickTexInterpreter as 5th argument, no more errors!



Hi, very nice function and useful updates, thank you. I still have a problem with tick labels though. Here is my command:

heatmap(results,window,labelROI,[], 'UseFigureColormap', false, 'NaNColor', [1 1 1], 'MinColorValue', -1, 'MaxColorValue', 1, 'Colorbar', true, 'Gridlines',':', 'ShowAllTicks',1, 'TickAngle', 90)

And this is the error:

Error using heatmap>createXTicks (line 364)
Not enough input arguments.

Error in heatmap>updateLabels (line 512)
axInfo.hXText = createXTicks(hAxes, p.TickAngle, xticks, xlabels(xticks));

Error in heatmap>resize (line 554)
updateLabels(hAxes(i), false);

Error using drawnow
Error while evaluating figure ResizeFcn

Can you help, please?
Thank you.

Ameya Deoras

Hi everyone. Thanks for the comments/feedback. I have updated the function with fixes and new features:
* It now correctly handles the labels for small matrices with labels.
* There are two options minColorLevel and maxColorLevel that allow you to specify how the data range maps to the color range. This is useful when comparing heatmaps and ensuring the same color-data mapping between heatmaps
* There is now a nanColor option that allows you to specify a special color for missing data elements.


this is an excellent contribution which I am finding most useful. Thanks.


Hi, nice function.

There seems to be a problem though with the labeling. In case of a small input-matrix (i.e. 5x5), one has to set "ShowAllTicks=1" otherwise the function will give an error when you also provide labels:
Subscript indices must either be real positive integers or logicals.

Error in heatmap2>setAxesLabels (line 280)

Error in heatmap2 (line 111)
[p, xlab, ylab, hXText] = setAxesLabels(p, xlab, ylab);

try it :)

Yair A

Yongqiang Zhou


Wow! Where I've been all this time! Thanks a lot.

Jonathan W

Excellent script. I have a question. Is there a way to color only certain values? Say, between 0 and 0.001 have one color, between 0.001 and 0.01 a different color, between 0.01 and 0.05 a different color, and everything else appear as white/blank? Thanks for your help.

Yilong Jia

Tung Le

Thanks for the code. It's very helpful.

I have a question. I want to plot two different heat maps but using the same color range so that one can compare them qualitatively. Is there anyway to do that with this code?
Thank you.

Franck Dernoncourt

Just one small detail: if the matrix has a dimension < 7 and that we don't set 'ShowAllTicks', true, it might screw the labels due to (heatmap.m)

if p.ShowAllTicks
xtick = 1:length(xlab);
xtick = get(p.hAxes, 'XTick');

> xtick = get(p.hAxes, 'XTick'); can return a larger array than xlab, thereby exploding at xlab(xtick).

Same for y I guess.


A very nice application of heat maps that can be used to quickly integrate heat maps into a project. Great work and thanks for sharing.


Jessica Lam

Shaohan Hu


Very useful tool to give the wow factor for presentations of data. Enables the creation of heat maps very quickly.

Bayes Zhang

Syed Abbas

Great! Very helpful

Gareth Thomas

Very nice:) I found it very useful

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!