Clear Filters
Clear Filters

Colors above a level is not shown in a contour plot

4 views (last 30 days)
This is my image and i want to draw boundaries to the clusters.
contourf(z_test_img, 40, 'EdgeColor','none'), colormap jet, colorbar
hold on
contourf(z_test_img, [z_thresh, z_thresh])
hold off
This was my attempt to add both the image with bıth contour lines and without contour lines in the same plot, and it works. But, with an issue, which is:
It shows the boundaries but the colors above level 2 stays the same. I couldn't figure it out on how to solve it. Any ideas?

Answers (2)

KSSV
KSSV on 26 Dec 2022
[X,Y,Z] = peaks(100) ;
[c,h] = contour(X,Y,Z,[6 6]) ;
% arrange the contour
c(:,1) = NaN ;
% plot
contourf(X,Y,Z,'EdgeColor','none') ; shading interp ; colorbar ; colormap(jet) ;
hold on
plot(c(1,:),c(2,:),'k')
  3 Comments
KSSV
KSSV on 26 Dec 2022
Edited: KSSV on 26 Dec 2022
You need to remove the outliers in c....either attach your data or remove the outliers by replacing them with NaN.
[X,Y,Z] = peaks(100) ;
[c,h] = contour(X,Y,Z,[4 8]) ;
% arrange the contour
idx = isoutlier(c(1,:)) ;
c(:,idx) = NaN ;
% plot
contourf(X,Y,Z,'EdgeColor','none') ; shading interp ; colorbar ; colormap(jet) ;
hold on
plot(c(1,:),c(2,:),'k')
Yigit
Yigit on 26 Dec 2022
I tried to do what you've told but it didn't work. Here's my data:

Sign in to comment.


Star Strider
Star Strider on 26 Dec 2022
The straight lines connecting the contours are because of the way the contour matrix (‘M’ here) are formatted.
Do something like this —
[X,Y,Z] = peaks;
figure
[M,C] = contourf(X, Y, Z, 'ShowText',1);
Levels = C.LevelList
Levels = 1×9
-6.5466 -6.0000 -4.0000 -2.0000 0 2.0000 4.0000 6.0000 8.0000
for k = 1:numel(Levels) % Get Contour (x,y) Coordinates For Each Contour Level
idx = find(M(1,:) == Levels(k));
ValidV = rem(M(2,idx),1) == 0;
StartIdx{k,:} = idx(ValidV);
VLen{k,:} = M(2,StartIdx{k});
end
figure
hold on
k1 = 4; % Choose The '-2' Contour (Levels(4))
% for k1 = 1:numel(Levels)
for k2 = 1:numel(StartIdx{k1})
idxv = StartIdx{k1}(k2)+1 : StartIdx{k1}(k2)+VLen{k1}(k2); % Index For Contour 'k1'
xv = M(1,idxv);
yv = M(2,idxv);
plot(xv, yv)
end
% end
hold off
axis([-3 3 -3 3])
title('Contours At The ‘-2’ Level')
See the contour documentation section on M for details.
.

Categories

Find more on Contour 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!