Highlight cells in heatmap

I created a heatmap where each cell represents a temperature value using the commands:
cdata = [values of temperature]
xvalues = [0,1,2,3,etc.]
yvalues = [0,1,2,3,etc.]
h = heatmap(xvalues,yvalues,cdata)
I need to:
  • command to highlight cells over a limit (for example 500)
  • Highlight cells within a given range (for example 300 to 800)
It is not important how the cells are highlighted, it can be a different colour (like green or blue), or a cross X on top of the cells.
Thanks for the help

 Accepted Answer

You can do so by creating a custom colormap. The code below shows a simple implementation.
xvalues = 1:5;
yvalues = 1:5;
cdata = randi(20,[5,5]); % range of values in cells < 20
cmap = [repmat([1 0 0],5,1)
repmat([1 1 1],5,1) % highlight all cells with values from 5-10 with white
repmat([0 0 0],10,1)]; % highlight all cells > 10 with black
heatmap(1:5,1:5,randi(20,[5 5]),'Colormap',cmap);

3 Comments

Hey Rishik thanks for your help!
I tried replace your example values with my array size and values, so that it would fit my actual needs
xvalues = 1:25 ;
yvalues = 1:25
cdata =
100 100 100 100 100 100 100 100 100
300 300 300 300 250 200 180 150 100
500 500 500 300 250 200 180 150 100
550 500 450 300 250 200 180 150 100
600 600 500 400 300 200 180 150 100
740 700 600 500 300 200 180 150 100
700 600 500 450 400 200 180 150 100
600 500 400 300 250 200 180 150 100
500 450 450 300 300 250 200 150 100
500 450 450 550 450 250 230 200 100
450 450 550 700 700 250 230 200 100
400 300 500 650 650 250 200 150 100
400 300 400 450 300 250 200 150 100
350 380 380 380 300 250 200 150 100
380 350 380 380 300 250 200 150 100
650 500 350 390 300 300 200 150 100
700 400 370 350 300 300 200 150 100
500 400 360 350 300 300 200 150 100
300 300 350 350 300 0 200 150 100
200 200 200 200 200 100 100 100 100
100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100
250 250 250 250 0 0 0 0 100
100 100 100 100 100 100 100 100 100
cmap = [repmat([0 0 0],200,1 ) %black background
repmat([1 1 1],200,1) % highlight all cells with values from 200-750 white
repmat([1 0 0],750,1)]; % highlight all cells > 750 with red (highest)
heatmap(1:25,1:25,cdata,'Colormap',cmap);
however this is the result (as you can see the map doesn't reflect the aimed ranges)
What am I doing wrong?
Your help is much appreciated
The second argument for repmat denotes the number of rows to be repeated for the RGB vector given in its first argument. So the cmap contains rows of RGB values which are equal in number to the range of values in the cdata (max-min). For visualisation, try imagining filling the colors on the colorbar from the bottom while creating a custom colormap.
max_val = max(cdata(:)) % get the max value from cdata
cmap = [repmat([1 0 0],200,1) % first 200 rows (0-199)
repmat([1 1 1],750-200,1) % 200-749
repmat([0 0 0],max_val-750,1)]; % >= 750
All clear! Thank you for your help!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2020b

Tags

Community Treasure Hunt

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

Start Hunting!