How can I draw a circular trend line in a heat map?

5 views (last 30 days)
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
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".
Cailey
Cailey on 5 May 2025
I apologize around that. I guess I don't have specific hard conditions right now, I am hoping that there was a common method for this. I am pretty new to these types of plots

Sign in to comment.

Accepted Answer

Matt J
Matt J on 5 May 2025
Edited: Matt J on 5 May 2025
If the trend line is supposed to be elliptical, you can use gaussfitn,
to fit a 2D gaussian surface to the map. Then use fcontour to overlay one of the Gaussian's elliptic isocontours (perhaps the half-maximum contour).
  2 Comments
Matt J
Matt J on 5 May 2025
Edited: Matt J on 5 May 2025
You're welcome, but if you find that it works, please Accept-click the answer.

Sign in to comment.

More Answers (1)

Adam Danz
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');

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!