Having issues with plotting a single level contour line on a matrix that has edges

조회 수: 28 (최근 30일)
Hi, I have a matrix with edges that is almost half filled with zeroes, the rest of the matrix is data for e.g. 'a' below. I want to draw a contour line say at level 4 however when I use the contour function, it shows me multiple lines which I'm trying to avoid.
a = [0 0 0 0 0;
0 0 0 0 5;
0 0 0 5 4;
0 0 5 4 3;
0 5 4 3 2;
5 4 3 0 0;]
For a more realistic depiction of my dataset, let's say the matrix 'a' when plotted with imagesc looks like below. All white pixels are zero values simply changed in colormap to display as white.
I want to draw a contour line let's say at level 400. I do using the command
[c,h] = contour(xAxis'*1e+3,yAxis,grid2D',[400 400],...
'--','LineWidth',1.5,'EdgeColor',cOrder(i,:),'ShowText','on');
h.LevelList=round(h.LevelList,2); %rounds label level to 3rd decimal place
clabel(c,h, 'labelspacing', 300); %print label spaced out
clabel(c,h,'fontsize', 8); %change font size of label
What I get as an output is
Here I get two contour lines which I'm assuming is because of the edge in the matrix? Is there a way to remove this and only get a single contour line. I've seen online that contour sometimes has issues with edges, what might be the easiest way to mitigate this?

채택된 답변

Star Strider
Star Strider 2023년 7월 28일
Unfortunately, enough information is missing from the posted code to prevent using it.
Getting the individual lines in a contour plot is actually straightforward —
[X,Y,Z] = peaks(20);
figure
[c,h] = contourf(X,Y,Z, [1 1]*3, '--', 'LineWidth',1.5);
AxLimV = axis;
StartIdx = find(c(1,:) == 3);
LenVal = c(2,StartIdx);
cv = turbo(numel(StartIdx));
figure
hold on
for k = 1:numel(StartIdx)
LineIdxVct{k} = StartIdx(k) + (1 : LenVal(k));
plot(c(1,LineIdxVct{k}), c(2,LineIdxVct{k}), 'Color',cv(k,:), 'LineWidth',2, 'DisplayName',["Line "+k])
end
hold off
axis(AxLimV)
legend('Location','best')
figure
[c,h] = contourf(X,Y,Z, [1 1]*3, 'EdgeColor','none');
hold on
plot(c(1,LineIdxVct{2}), c(2,LineIdxVct{2}), '--', 'Color','k', 'LineWidth',2) % Plot Border Of Contour #2
hold off
This is slightly involved, however not difficult. (Plotting the second figure that illustrates the individual contours is obviously not necessary, although calculating ‘LineIdxVct’ is. I plotted it here only to illustrate the essential approach.)
.

추가 답변 (1개)

Les Beckham
Les Beckham 2023년 7월 28일
From your simple example, it can be seen that there actually are two places where the interpolated surface passes through the level of 4, on the right between the fives and the fours and on the left between the fives and the zeros.
Expecting contour to ignore that somehow seems odd.
a = [0 0 0 0 0;
0 0 0 0 5;
0 0 0 5 4;
0 0 5 4 3;
0 5 4 3 2;
5 4 3 0 0;];
% Note: flip a so that it is in the same orientation in the plot as the actual
% matrix (the block of zeros is in the upper left)
contour(flipud(a), 'ShowText', true);
axis equal
grid on

카테고리

Help CenterFile Exchange에서 Contour Plots에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by