Having issues with plotting a single level contour line on a matrix that has edges
Show older comments
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?
Accepted Answer
More Answers (1)
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
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!


