Colors above a level is not shown in a contour plot

6 次查看(过去 30 天)
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?

回答(2 个)

KSSV
KSSV 2022-12-26
[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 个评论
KSSV
KSSV 2022-12-26
编辑:KSSV 2022-12-26
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')

请先登录,再进行评论。


Star Strider
Star Strider 2022-12-26
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.
.

类别

Help CenterFile Exchange 中查找有关 Contour Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by