# How to create "Heatmap" of difference between scatter plots

11 views (last 30 days)
Malachi on 16 Feb 2023
Answered: Jaswanth on 8 Jan 2024
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);
Malachi on 17 Feb 2023
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');

Jaswanth on 8 Jan 2024
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.