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

12 vues (au cours des 30 derniers jours)
Cailey
Cailey le 5 Mai 2025
Modifié(e) : Adam Danz le 5 Mai 2025
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 commentaires
Torsten
Torsten le 5 Mai 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 le 5 Mai 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

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 5 Mai 2025
Modifié(e) : Matt J le 5 Mai 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 commentaires
Cailey
Cailey le 5 Mai 2025
Thank you, I will look into this!
Matt J
Matt J le 5 Mai 2025
Modifié(e) : Matt J le 5 Mai 2025
You're welcome, but if you find that it works, please Accept-click the answer.

Connectez-vous pour commenter.

Plus de réponses (1)

Adam Danz
Adam Danz le 5 Mai 2025
Modifié(e) : Adam Danz le 5 Mai 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');

Catégories

En savoir plus sur Data Distribution Plots dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by