How to get heat maps (color the surface) in Matlab?

I want to develop a heat map of moisture content with color bar like the attachced picture for a rectangular box of length 73''*Width 31''.I have the moisture content from 18 locations in the box.The moisture values at these 18 locations are given with their x and y coordinates in the excel file.These maps need to be generated by using moisture content at the x& y locations and linearly interpolated values in the spacing between the locations. Can anyone help me ?

 Accepted Answer

T = readtable('heat_map.xlsx','VariableNamingRule','preserve')
T = 18×3 table
Length (x) width (y) Moisture Content (%) __________ _________ ____________________ 12 10 15 12 20 15.23 12 30 15.46 24 10 15.69 24 20 15.92 24 30 16.15 36 10 16.38 36 20 16.61 36 30 16.84 48 10 17.07 48 20 17.3 48 30 17.53 60 10 17.76 60 20 17.99 60 30 18.22 72 10 18.45
nX = numel(unique(T.(1)));
nY = numel(unique(T.(2)));
X = reshape(T.(1),nY,nX);
Y = reshape(T.(2),nY,nX);
Z = reshape(T.(3),nY,nX);
surface(X,Y,Z,'FaceColor','interp','EdgeColor','none')
colormap(flipud(turbo()))
colorbar
xlim([min(X(:)) max(X(:))])
ylim([min(Y(:)) max(Y(:))])

10 Comments

Thank you.I wanted to show the color plot for the whole box (X=73'',y=31'').I tried changing the axis limits like below,but giving me a plot like the attached picture.Can you please help? How I can have x=0-73 in x axis and y=0-31 in y axis?And full 73*31 box is color plotted? Is there a way to get the axis according to their actual size,like x=73 inch is more than twice in size compared to y=31 inch.
xlim([0 73])
ylim([0 31])
You're welcome!
"And full 73*31 box is color plotted?"
What color should the empty regions (i.e., anything below y=10 or to the left of x=12) be? Do you want to extrapolate the Z values to those regions? (If so, see below.)
"Is there a way to get the axis according to their actual size"
I think you want axis equal.
T = readtable('heat_map.xlsx','VariableNamingRule','preserve');
nX = numel(unique(T.(1)));
nY = numel(unique(T.(2)));
X = reshape(T.(1),nY,nX);
Y = reshape(T.(2),nY,nX);
Z = reshape(T.(3),nY,nX);
X_plot = 0:max(X(:));
Y_plot = 0:max(Y(:));
Z_plot = interp2(X,Y,Z,X_plot,Y_plot.','spline');
surface(X_plot,Y_plot,Z_plot,'FaceColor','interp','EdgeColor','none')
colormap(flipud(turbo()))
colorbar
axis equal
xlim(X_plot([1 end]))
ylim(Y_plot([1 end]))
Thank you very much!
Hello ,
I used another data set(attached).At three corner points (attached picture),it's giving very lower values compared to rest of the box,looks like it's not doing the extrapolation accurately. The value is not expected to get that lower in the corner points. Do you have any suggestion gow to improve the interpolation and extrapolation,so that it shows a value close to nearest known point?I mainly wanted to rely on the linear method of interpolation and extrapolation.The expected values are above 17% all through the box,but with the current code I am getting as low as 11-12%!The code is given below for your reference.
T = readtable('heat_map_control_layer_F.xlsx','VariableNamingRule','preserve');
nX = numel(unique(T.(1)));
nY = numel(unique(T.(2)));
X = reshape(T.(1),nY,nX);
Y = reshape(T.(2),nY,nX);
Z = reshape(T.(3),nY,nX);
X_plot = 0:72;
Y_plot = 0:30;
Z_plot = interp2(X,Y,Z,X_plot,Y_plot.','spline');
surface(X_plot,Y_plot,Z_plot,'FaceColor','interp','EdgeColor','none')
colormap(flipud(turbo()))
colorbar
axis equal
xlim(X_plot([1 end]))
ylim(Y_plot([1 end]))
xlabel('Length (in)')
ylabel('Width (in)')
Do you know how I can fix the color code bar (for better comparison in between different plots) so that in every plot it shows the same range,for instance 15-25? Thank you for helping!
That data doesn't appear to correspond to the picture (different max X and Y), but here's how you can do linear interpolation and extrapolation:
T = readtable('heat_map_control_layer_F.xlsx','VariableNamingRule','preserve');
nX = numel(unique(T.(1)));
nY = numel(unique(T.(2)));
X = reshape(T.(1),nY,nX);
Y = reshape(T.(2),nY,nX);
Z = reshape(T.(3),nY,nX);
SI = scatteredInterpolant(X(:),Y(:),Z(:));
[X_plot,Y_plot] = meshgrid(0:max(X(:)),0:max(Y(:)));
Z_plot = SI(X_plot,Y_plot);
surface(X_plot,Y_plot,Z_plot,'FaceColor','interp','EdgeColor','none')
colormap(flipud(turbo()))
colorbar
axis equal
xlim(X_plot([1 end]))
ylim(Y_plot([1 end]))
And to set the colorbar limits the same every time use the clim function (or caxis in versions before R2022a).
clim([15 25])
Thank you.
I changed code little,given below (instead of max x, put 72 ;instead of max y, put 30) to show the plot for 72''*30''. Please see the attached picture.The dot points are my known points.I know 18 values in between x=6-66,y=5-25,shown as dot points in the atatched picture.So,inside this region I want to do linear interpolation.But outside this region I want to obtain linear extrapolation(please see the attahced picture for clarity). I am using the following code.Do you thing the code is doing interpolation for the inside box (bottom image in the hand drawn attachment) and extrapolation in the region between two boxes(bottom image in the hand drawn attachment)? I am getting the following plot,I feel it's not doing correctly.Please check and provide correction.Thanks again for all the help
Do you know how I can show the 18 known points as dots in the plot?
T = readtable('heat_map_control_layer_F.xlsx','VariableNamingRule','preserve');
nX = numel(unique(T.(1)));
nY = numel(unique(T.(2)));
X = reshape(T.(1),nY,nX);
Y = reshape(T.(2),nY,nX);
Z = reshape(T.(3),nY,nX);
SI = scatteredInterpolant(X(:),Y(:),Z(:));
[X_plot,Y_plot] = meshgrid(0:72,0:30);
Z_plot = SI(X_plot,Y_plot);
surface(X_plot,Y_plot,Z_plot,'FaceColor','interp','EdgeColor','none')
colormap(flipud(turbo()))
colorbar
axis equal
xlim(X_plot([1 end]))
ylim(Y_plot([1 end]))
xlabel('Length (in)')
ylabel('Width (in)')
clim([18 23])
"Do you [think] the code is doing interpolation for the inside box (bottom image in the hand drawn attachment) and extrapolation in the region between two boxes(bottom image in the hand drawn attachment)"
Yes, now that you've changed from using interp2(_,'spline'), which does spline interpolation and extrapolation, to using scatteredInterpolant(_,'linear','linear') (the defaults), which does linear interpolation and extrapolation.
T = readtable('heat_map_control_layer_F.xlsx','VariableNamingRule','preserve');
nX = numel(unique(T.(1)));
nY = numel(unique(T.(2)));
X = reshape(T.(1),nY,nX);
Y = reshape(T.(2),nY,nX);
Z = reshape(T.(3),nY,nX);
SI = scatteredInterpolant(X(:),Y(:),Z(:));
[X_plot,Y_plot] = meshgrid(0:72,0:30);
Z_plot = SI(X_plot,Y_plot);
surface(X_plot,Y_plot,Z_plot,'FaceColor','interp','EdgeColor','none')
colormap(flipud(turbo()))
colorbar
axis equal
xlim(X_plot([1 end]))
ylim(Y_plot([1 end]))
xlabel('Length (in)')
ylabel('Width (in)')
clim([18 23])
"Do you know how I can show the 18 known points as dots in the plot?"
set(gca(),'NextPlot','add','SortMethod','childorder')
plot(X(:),Y(:),'.')
Note that X and Y are not as shown in the sketch (0 and 72 instead of 6 and 66; 30 instead of 25):
X,Y
X = 3×6
0 18 30 42 54 72 0 18 30 42 54 72 0 18 30 42 54 72
Y = 3×6
5 5 5 5 5 5 15 15 15 15 15 15 30 30 30 30 30 30
Thank you very much!

Sign in to comment.

More Answers (0)

Asked:

on 14 Mar 2024

Commented:

on 15 Mar 2024

Community Treasure Hunt

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

Start Hunting!