필터 지우기
필터 지우기

Colors above a level is not shown in a contour plot

조회 수: 5 (최근 30일)
Yigit
Yigit 2022년 12월 26일
답변: Star Strider 2022년 12월 26일
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')
Yigit
Yigit 2022년 12월 26일
I tried to do what you've told but it didn't work. Here's my data:

댓글을 달려면 로그인하십시오.


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