How to create "Heatmap" of difference between scatter plots

조회 수: 6 (최근 30일)
Malachi
Malachi 2023년 2월 16일
답변: Jaswanth 2024년 1월 8일
I have two scatter plots. I would like to visualize the difference or shift between the two. For example, if a point (imgx,imgy) shifts away from (0,0) with respect to its closest point (ux,uy), I would like the region aroun this point to be colored, and its intensity dependent on the amount shifted. If it shifts toward (0,0), then a different color. I would like this to measure the change in pythagorean distance and color accordingly.
A = -5:5;
B = -5:5;
objx = A';
objy = B';
ux = repelem(objx,11,1);
ux = ux(:);
uy = repelem(objy,1,11);
uy = uy(:);
imgx = ux + (2*rand(size(ux)) - 1)*0.1;
imgy = uy + (2*rand(size(uy)) - 1)*0.1;
scatter(ux,uy);
hold on
scatter(imgx,imgy);
  댓글 수: 1
Malachi
Malachi 2023년 2월 17일
I was able to create something like what I was looking for. I tried heatmap and pcolor, but imagesc got me what I wanted, I think.
A = -5:5;
B = -5:5;
objx = A';
objy = B';
ux = repelem(objx,11,1);
ux = ux(:);
uy = repelem(objy,1,11);
uy = uy(:);
imgx = ux + (2*rand(size(ux)) - 1)*0.1;
imgy = uy + (2*rand(size(uy)) - 1)*0.1;
%imgx = ux + (rand(size(ux)))*0.1;
%imgy = uy + (rand(size(uy)))*0.1;
robj = sqrt(ux.^2 + uy.^2);
rimg = sqrt(imgx.^2 + imgy.^2);
delr = rimg-robj;
%pcolor(delr);
diff = zeros(11);
i = 1;
for x = A
for y = B
robj = sqrt(ux(i).^2 + uy(i).^2);
rimg = sqrt(imgx(i).^2 + imgy(i).^2);
diff(x+6,y+6) = rimg-robj;
% diff(x+6,y+6) = sqrt((ux(i) - imgx(i)).^2 + (uy(i) - imgy(i)).^2);
i = i+1;
end
end
% figure
% pcolor(diff);
figure
scatter(ux,uy);
hold on
scatter(imgx,imgy);
% figure
% heatmap(diff);
figure
imagesc(diff);
colormapeditor
save('CustomColormap','CustomColormap');

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

답변 (1개)

Jaswanth
Jaswanth 2024년 1월 8일
Hi,
I understand that you would like to visualize the shift between two scatter plots highlighted with different colour intensities based on if they are shifting towards the origin or away from the origin by calculating the Pythagorean distance of the shift. I have gone through the codes you have provided and have modified them to incorporate the shift of individual points from their original positions to their new positions, along with the direction and intensity of these shifts.
Please refer to following modified code for above explained implementation:
% Parameters from existing code
% Calculate the shift distances
shift_distances = sqrt((imgx - ux).^2 + (imgy - uy).^2);
% Determine the direction of the shift (towards or away from the origin)
original_distances = sqrt(ux.^2 + uy.^2);
new_distances = sqrt(imgx.^2 + imgy.^2);
shift_direction = new_distances - original_distances;
% Create a colormap based on the shift distances and direction
shift_intensity = min(max(shift_distances / max(shift_distances), 0), 1); % Normalize
colors = zeros(length(shift_intensity), 3);
colors(shift_direction > 0, 1) = shift_intensity(shift_direction > 0); % Red for away
colors(shift_direction <= 0, 2) = shift_intensity(shift_direction <= 0); % Green for towards
% Plot the original points
scatter(ux, uy, 36, 'k', 'filled');
hold on
% Overlay the shifted points with the colormap
scatter(imgx, imgy, 36, colors, 'filled');
% Add a colorbar if needed
colormap([linspace(0, 1, 256)', linspace(1, 0, 256)', zeros(256, 1)]); % Red to green colormap
c = colorbar;
c.Label.String = 'Shift Intensity';
Hope the solution provided above is helpful.
Thanks.

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by