Remove specific contours or make them invisible

9 views (last 30 days)
Hi,
I am creating a contour plot with contours at different levels.
How can I programmatically turn off certain contours or make them invisible. All outer contours that do not have an inner contour should be invisible or deleted from the plot.
For example in this case I create contours around -6 and -3. I want to remove the left contour at -3.
Thanks.
[X,Y,Z] = peaks(1000);
figure(1),hold on
view(2);
colorbar('vert');
s = surf(X,Y,Z,'Facealpha',0.75,'Edgecolor','none');
% threshold = -0.3*max(Z,[],'all');
threshold1 = [-6, -6];
threshold2 = [-3, -3];
[c1,h1] = contour(X,Y,Z,[threshold1 threshold1],'-k','Linewidth',5);
[c2,h2] = contour(X,Y,Z,[threshold2 threshold2],'-k','Linewidth',5);
hold off

Accepted Answer

Bjorn Gustavsson
Bjorn Gustavsson on 25 Oct 2022
The easiest way is to continue with the algorithm where you save the level-2-contours with a level one contour inside. Then you can reasonably easy remove all the level-2-contours, if you read the help and documentation to contour you will see that the second output is the handle (matlab type: matlab.graphics.chart.primitive.Contour) You can go in to the properties of that handle. But to me it seems easier to just delete that one and then plot the ones from the contour-coordinates you have saved:
[X,Y,Z] = peaks(1000);
figure(1),hold on
view(2);
colorbar('vert');
s = surf(X,Y,Z,'Facealpha',0.75,'Edgecolor','none');
% threshold = -0.3*max(Z,[],'all');
threshold1 = [-6, -6];
threshold2 = [-3, -3];
[c1,h1] = contour(X,Y,Z,[threshold1 threshold1],'-k','Linewidth',5);
[c2,h2] = contour(X,Y,Z,[threshold2 threshold2],'-k','Linewidth',5);
hold off
% Insert code to extract the contour-coordinates you want
[c2X,c2Y] = your_contour_selector(c2,c1);
delete(h2)
for i1 = 1:numel(c2X)
plot(c2X{i1},c2Y{i1},'k','linewidth',5);
end
HTH
  2 Comments
Konvictus177
Konvictus177 on 25 Oct 2022
Oh wow. Thanks. Sometimes you don't see the solution when you are lost in the code. Since I have extracted all the contour-coordinates for both thresholds I can easily plot them myself for where I have an inner and outer contour.
Bjorn Gustavsson
Bjorn Gustavsson on 25 Oct 2022
Yeah, that is a "not uncommon" problem - I try to get my students to print out their code on paper and draw-sketch-write their algorithm-improvements and code corrections over those printouts, often that step of change-of-media forces you to lift the gaze and give you a birds-eye view. Awfully difficult to do once one are stuck in a "I'll force this bastard of a script into submission" mode...
All the best.

Sign in to comment.

More Answers (0)

Categories

Find more on Contour Plots in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!