Heat maps with 3 variable

조회 수: 33(최근 30일)
편집: LO 2021년 6월 14일
hello all, I am fairly new to Matlab. What's the best way to create a scatter plot where for example x axis is year, y axis is month and the colour of each point is dependent on average temperature? Like a heat map. Any help is appreciated, I may be missing something obvious. Thanks in advance, Huw
댓글 수: 1표시숨기기 없음
Scott MacKenzie 2021년 6월 14일
You can review your options at Types of MATLAB Plots. Problably contour or contourf is what you want.

댓글을 달려면 로그인하십시오.

채택된 답변

LO 2021년 6월 14일
편집: LO 2021년 6월 14일
you can use the function "scatter3", if you have 3 variables (this will display a scatter plot of x and y and z on the 3rd axis). If you use the 3rd variable as color variable (not as z but as "c", see documentation of scatter3) you can color code x and y based on your 3rd var. Use view(2) to flatten the plot and avoid the 3D if you want a heatmap like plot.
S = 5; % size of the dots
% this may or not work depending on the values of Z (you may convert them to fit,
% either multiplying them or rescaling them so that they are > 0 and < 1.
% for example z = z/max(z), this vector will now contain normalized values
scatter3(x,y,zeros(numel(x),1),S,z,'filled','MarkerEdgeColor','none')
view(2)
I alternatively use imagesc (as in this example by Johnathan Epperl), you can adjust the color intensity changing the limits of the color axis (caxis, see documentation). You can visualize a colorbar using the "colorbar" command.
See if it works for your purpose
x = linspace(0,2*pi);
% y-axis
y = linspace(0,2*pi);
% a mesh because
[X,Y] = meshgrid(x,y);
% you need data at every possible x-y combination
Z = sin(X).*atan(Y);
% that scales the Z-values and plots a heatmap
imagesc(x,y,Z)
% choose a colormap of your liking
colormap hot
colorbar;
another alternative (assuming x,y,z are your vars)
range_x = max(x)-min(x);
range_y = max(y)-min(y);
average = mean(z);
heatmap = average*ones(100, 100);
for k = 1 : length(x)
X = round( (x(k) - min(x)) * 100 / range_x ) + 1;
Y = round( (y(k) - min(y)) * 100 / range_y ) + 1;
heatmap(Y, X) = z(k);
end
imshow(heatmap,[]);
colormap('hot');
colorbar;

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by