Clear Filters
Clear Filters

How to create a heatmap which would represent superimposed rectangles of different dimensions?

13 views (last 30 days)
I cannot solve the following problem:
My variable 'data' includes values that represent lengths of rectangles edges. I would like to plot these rectangles in a superimposed manner such that the heatmap shows which areas of rectangles overlap the most. My code below does that. However, I do not know how to transform this code into solution with heatmap which would resemble something like on the attached picture.
How to display this as a heatmap with colorbar on the right? I would be very greatfule for your help.
My code:
f = figure(1);
data = [6.65 1.31 7.33 2.98 3.89 19.06 8.09];
for i = 1:length(data(1,1:end))
dimen = data(i)/2;
pgon = polyshape([0-dimen 0-dimen 0+dimen 0+dimen],[0+dimen 0-dimen 0-dimen 0+dimen]);
pg = plot(pgon);
pg.FaceColor = [1 0 0]; pg.FaceAlpha = 0.15;
pg.EdgeColor = [0 0 0]; pg.EdgeAlpha = 0.15;
hold on
axis([-10 10 -10 10])
end
dimen = 2/2;
pgon = polyshape([0-dimen 0-dimen 0+dimen 0+dimen],[0+dimen 0-dimen 0-dimen 0+dimen]);
pg = plot(pgon);
pg.FaceColor = [1 0 0]; pg.FaceAlpha = 0.15;
pg.EdgeColor = [0 0 0]; pg.EdgeAlpha = 1;
title('data A')
hold off

Answers (1)

Vinayak Choyyan
Vinayak Choyyan on 8 Feb 2023
Hello Wax,
As per my understanding, you would like to add a colorbar to your plot. Your plot consists of seven superimposed rectangles of different dimensions. From the code you provided, I see that you are using the ‘polyshape()’ function to draw rectangles with transparency (alpha value of 0.15). Since these rectangles are superimposed, the transparency of the rectangles results in different shades of red.
To add a colorbar to your plot, you can ‘colorbar’ function in MATLAB. Using the ‘colormap’ function, you can give custom colors to the colormap which creates the colorbar.
Unfortunately, ‘colorbar’ and ‘colormap’ do not support RGBA color format. To create a custom colormap, one needs to define a three-column matrix of values between 0.0 and 1.0. Each row defines a three-element RGB triplet. You can refer to the following web page to know more about RGBA and RGB HTML RGB and RGBA Colors (w3schools.com).
As a workaround to the issue you are facing, I would like to suggest you can give the resultant RBG values of the shades of red as input to the map. Please refer to the following code to see an example of the same. I have used the ‘rectangle’ function instead of the ‘polygon’ function to showcase an alternate approach to plot rectangles. I have also sorted the data, in descending order, before plotting so that the biggest rectangle is plotted first and then the smaller rectangles are plotted on top of the bigger rectangles in order of biggest to smallest. This is because I did not want a bigger rectangle being superimposed over a smaller one and resulting in the smaller rectangle from being hidden as these rectangles do not have any transparency.
f = figure(1);
data = [6.65 1.31 7.33 2.98 3.89 19.06 8.09];
data=sort(data,"descend");
map=zeros(length(data),3);
for i = 1:length(data)
dimen = data(i)/2;
map(i,:)=[1-i/length(data) 0 0]; %a different shade of red for each rectangle
rectangle("Position",[0-dimen 0-dimen data(i) data(i)], "FaceColor",map(i,:),EdgeColor=[0 0 0])
hold on
axis([-10 10 -10 10])
end
title('data A')
hold off
colormap(map)
colorbar
I hope this resolves the issue you are facing. Please refer to the following documents to read more on the following:

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!