How to create "Heatmap" of difference between scatter plots
Show older comments
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 Comment
Malachi
on 17 Feb 2023
Answers (1)
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.
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!