Having issues with plotting a single level contour line on a matrix... (2024)

7 views (last 30 days)

Show older comments

Jyoti Mangal on 28 Jul 2023

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/2001937-having-issues-with-plotting-a-single-level-contour-line-on-a-matrix-that-has-edges

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/2001937-having-issues-with-plotting-a-single-level-contour-line-on-a-matrix-that-has-edges

Commented: Star Strider on 1 Aug 2023

Accepted Answer: Star Strider

Open in MATLAB Online

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.

Having issues with plotting a single level contour line on a matrix... (2)

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

Having issues with plotting a single level contour line on a matrix... (3)

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?

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Star Strider on 28 Jul 2023

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2001937-having-issues-with-plotting-a-single-level-contour-line-on-a-matrix-that-has-edges#answer_1280272

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2001937-having-issues-with-plotting-a-single-level-contour-line-on-a-matrix-that-has-edges#answer_1280272

Open in MATLAB Online

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);

Having issues with plotting a single level contour line on a matrix... (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')

Having issues with plotting a single level contour line on a matrix... (6)

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

Having issues with plotting a single level contour line on a matrix... (7)

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.)

.

2 Comments

Jyoti Mangal on 1 Aug 2023

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/2001937-having-issues-with-plotting-a-single-level-contour-line-on-a-matrix-that-has-edges#comment_2835427

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/2001937-having-issues-with-plotting-a-single-level-contour-line-on-a-matrix-that-has-edges#comment_2835427

Thank you! This is exactly what I was looking for!

Star Strider on 1 Aug 2023

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/2001937-having-issues-with-plotting-a-single-level-contour-line-on-a-matrix-that-has-edges#comment_2835452

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/2001937-having-issues-with-plotting-a-single-level-contour-line-on-a-matrix-that-has-edges#comment_2835452

As always, my pleasure!

Sign in to comment.

More Answers (1)

Les Beckham on 28 Jul 2023

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2001937-having-issues-with-plotting-a-single-level-contour-line-on-a-matrix-that-has-edges#answer_1280267

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2001937-having-issues-with-plotting-a-single-level-contour-line-on-a-matrix-that-has-edges#answer_1280267

Open in MATLAB Online

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

Having issues with plotting a single level contour line on a matrix... (11)

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABGraphics2-D and 3-D PlotsContour Plots

Find more on Contour Plots in Help Center and File Exchange

Tags

  • contour
  • edge
  • plot
  • imagesc
  • contour levels

Products

  • MATLAB

Release

R2023a

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Having issues with plotting a single level contour line on a matrix... (12)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

Having issues with plotting a single level contour line on a matrix... (2024)
Top Articles
Latest Posts
Article information

Author: Greg Kuvalis

Last Updated:

Views: 6644

Rating: 4.4 / 5 (75 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Greg Kuvalis

Birthday: 1996-12-20

Address: 53157 Trantow Inlet, Townemouth, FL 92564-0267

Phone: +68218650356656

Job: IT Representative

Hobby: Knitting, Amateur radio, Skiing, Running, Mountain biking, Slacklining, Electronics

Introduction: My name is Greg Kuvalis, I am a witty, spotless, beautiful, charming, delightful, thankful, beautiful person who loves writing and wants to share my knowledge and understanding with you.