How can I draw a circular trend line in a heat map?
7 views (last 30 days)
Show older comments
I am working on some analysis for a visual search experiment and am looking to make a trend line around the heat map. Something like this (I drew the oval by hand):

I am having trouble with how to do this, but I need it to help find proportions of the horizontal and vertical heights for my analysis.
Thank you!
2 Comments
Torsten
on 5 May 2025
What are the hard conditions about the region you want to encircle ? So far, you vaguely describe it as "trend line around the heat map".
Accepted Answer
More Answers (1)
Adam Danz
on 5 May 2025
Edited: Adam Danz
on 5 May 2025
This solution plots a contour line that was computed at a specific level using smoothed data and is plotted on top of the original image data.
1. Create noisy demo data. In this example, the 2D distribution is slightly elliptical.
x = linspace(-3,3,50);
data = exp(-(.6*x.^2+x'.^2)/2) + (rand(50)-.5)./4;
% For a circular 2D distribution: data = exp(-(x.^2+x'.^2)/2) + (rand(50)-.5)./4;
2. Plot the data as an image
m = imagesc(data);
axis equal tight
cb = colorbar;
3. Use contour to create a contour line at a specified level. I chose a level where green starts to turn into blue in the colorbar at y=0.5. Contour uses the marching squares algorithm which results in the thin black border line in the results below.
level = 0.5;
hold on
[cm,h] = contour(data,level+zeros(1,2), 'LineWidth',1,'color','k');
4. Re-compute the contour using smoothed data. I'm using imgaussfilt from the Image Processing Toolbox but you could also use smoothdata2. Note that smoothing the data may change the range of level. Change how much smoothing is applied by reducing or increasing the 2nd argument in imgaussfilt or the third argument in smoothdata2.
dataSmooth = imgaussfilt(double(data),2);
% Alternative: dataSmooth = smoothdata2(data,'gaussian');
[cm2,h2] = contour(dataSmooth,level+zeros(1,2), 'LineWidth',3,'color','r');
Here's an example of the same approach applied to a more complex terrain. I reduced the guassian filter from 2 sd to 1 (2nd argument in imgaussfilt).
data = peaks(50) + (rand(50)-.5);
figure;
imagesc(data)
colorbar
level = 2;
hold on
[cm,h] = contour(data,level+zeros(1,2), 'LineWidth',1,'color','k');
dataSmooth = imgaussfilt(double(data),1);
% Alternative: dataSmooth = smoothdata2(data,'gaussian');
[cm2,h2] = contour(dataSmooth,level+zeros(1,2), 'LineWidth',3,'color','r');
0 Comments
See Also
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!
