how to applay heatmap for trajectory line?

4 views (last 30 days)
I want to know how to heatmap line on trajectory(x-y corrdinate)?
How can I deal with duplicate data?
%% フラクタル次元結果 ヒートマップで描画
clear all;
close all;
%% data for heatmap
load('FractalDimension_Double5','f')
FraD5 = f;
%% trajectory data
load('Double5.mat')
%% XY-coordinate
X1 = sin(Theta1);
Y1 = -cos(Theta1);
X2 = X1 + sin(Theta2);
Y2 = Y1 - cos(Theta2);
Tra = [X1,Y1,X2,Y2];
x1 = zeros(10000,1);
y1 = zeros(10000,1);
x2 = zeros(10000,1);
y2 = zeros(10000,1);
F = zeros(10000,1);
for i=1:10000
x1(i) = X1(i);
y1(i) = Y1(i);
x2(i) = X2(i);
y2(i) = Y2(i);
F(i) = FraD5(i);
end
%% draw trajectory
figure(1)
plot(x1,y1,'bo','MarkerFaceColor','b','MarkerSize',5)
hold on
plot(x2,y2,'bo','MarkerFaceColor','b','MarkerSize',5)
grid on
%% I want to draw heatmap curve on trajectory
figure(2)
%[x1i,y1i] = meshgrid(x1,y1);
%[x2i,y2i] = meshgrid(x2,y2);
%f = scatteredInterpolant(x1,y1,F);
%h = scatteredInterpolant(x2,y2,F);
%Fi=f(x1i,y1i);
%Fi =h(x2i,y2i);
%J = figure(2);
%scatter(x1,y1,70,F,'filled')
%hold on
%scatter(x2,y2,70,F,'filled')
%J = gca;
%J.FontSize = 20;
%J.FontName = 'Times New Roman';
%ylabel('Y','fontSize',18);
%xlabel('Y','fontSize',18);
%colormap(jet(256))
%colorbar

Answers (1)

Vedant Shah
Vedant Shah on 2 May 2025
The original implementation utilizes the plot function, which produces a single-colored line along the x-y coordinate trajectory. However, the desired outcome is to visualize the trajectory as a heatmap, where color varies according to a third variable “F”, thus providing more insight into the data distribution along the path. Additionally, the presence of duplicate (x, y) coordinate pairs can lead to warnings or inaccuracies in visualization. Both the issues can be resolved as below:
Resolving Duplicate Coordinate Warnings:
To address this, it is advisable to first identify and remove duplicate points. This can be accomplished by extracting unique (x, y) pairs and averaging the corresponding F values for each unique coordinate. The following approach can be adopted:
[XY1_unique, ~, idx1] = unique([x1, y1], 'rows', 'stable');
F1_unique = accumarray(idx1, F, [], @mean);
x1_unique = XY1_unique(:,1);
y1_unique = XY1_unique(:,2);
This process ensures that only unique coordinate pairs remain, with each F value representing the average of all occurrences for that coordinate. The same procedure should be applied to other coordinate sets, such as (x2, y2).
Plotting heatmap along the trajectory:
For visualization, the surf function in MATLAB can be leveraged to create a heatmap effect along the trajectory. By constructing a pseudo-surface where both columns of the x and y inputs are identical, and the z-dimension is set to zero (for a 2D plot), the edge color of the line can be interpolated based on the F values:
surf([x1_unique x1_unique]', [y1_unique y1_unique]', [zeros(size(x1_unique)) zeros(size(x1_unique))]', ...
[F1_unique F1_unique]', ...
'FaceColor', 'none', ...
'EdgeColor', 'interp', ...
'LineWidth', 2);
This approach ensures that the trajectory is visualized as a colored line, with the color varying smoothly according to the associated F values, effectively creating a heatmap along the path. The same modification can be applied to other coordinate sets as well.
After incorporating these modifications into the code, the resulting output obtained is as shown below:
For further information, refer to the following MATLAB documentation links:

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!