Contour plot

조회 수: 11 (최근 30일)
Falk
Falk 2011년 5월 24일
답변: Yusuf Yilmaz 2021년 11월 10일
Hi,
How can I make contour plots that do not 'interpolate'? For example, suppose I have the matrix a = [0 0 0 0; 0 1 1 0; 0 1 1 0; 0 0 0 0], which I would like to look like a square when I use contour(a,1) (the ,1 is used in order to have only one contour line). However, Matlab outputs an octagon-like shape, i.e. it kind of rounds the corners. I have seen that certain Matlab based software can do such contour plots, but I can't access the code and haven't been able to get it to work myself. Any help would be greatly appreciated!
Thanks,
Falk

채택된 답변

Sean de Wolski
Sean de Wolski 2011년 5월 24일
Multiple calls to line. You'll have to write some type of wrapper function but it should be doable.
  댓글 수: 1
Falk
Falk 2011년 5월 25일
Thanks, great idea! Definitely more work than just using contour, but works fine!

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

추가 답변 (4개)

Arnaud Miege
Arnaud Miege 2011년 5월 24일
Try this:
contour(a,[1 1])
HTH,
Arnaud
  댓글 수: 2
Falk
Falk 2011년 5월 24일
Thanks a lot, but it's not quite what I was looking for. I guess I didn't explain clear enough. If I use the normal contour and overlay it on my matrix...
figure(1); imagesc(a); colormap gray; hold on; contour(a,1,'color',[1 0 0]);
... then I get the kind of 'cut' corners. However, when I use your version...
figure(2); imagesc(a); colormap gray; hold on; contour(a,[1 1],'color',[1 0 0]);
... it doesn't fit the matrix but is smaller. Any ideas? I'm actually working with arbitrary shapes (the square was just an example), so it would be quite nice if the contour really traced each outer pixel...
Thanks,
Falk
Arnaud Miege
Arnaud Miege 2011년 5월 24일
It seems that the contour matrix is computed automatically and is read-only, so you can't edit it - at least I haven't found a way to do it. Maybe contour isn't the right way to go about it?

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


Sean de Wolski
Sean de Wolski 2011년 5월 24일

If instead of a contour plot you could deal with an edge, this might work:

Lrgb = label2rgb(bwperim(kron(a,ones(10)))); %expanding with 10x10 to create an edge
imshow(Lrgb) 
%IPT required
  댓글 수: 1
Falk
Falk 2011년 5월 24일
Thanks a lot to both of you! The bwperim is an interesting idea, but still isn't what I am looking for, as it gives me a non-connected outline (i.e. filled boundary pixels) that is as thick as each pixel. Ideally, I would like to have a connected line (as thin as the contour line) that traces the boundary (and can be overlaid on another image / plotted matrix). Quite specific wishes, I know... Any further ideas?
Thanks,
Falk

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


Matt Tearle
Matt Tearle 2011년 5월 24일
Massive hack that works in this case (not sure how generalizable it will be to whatever your application is):
a(a==0) = NaN;
mesh(a)
set(gca,'View',[0,90],'XGrid','off','YGrid','off')
  댓글 수: 2
Falk
Falk 2011년 5월 25일
Thanks, that's a cool trick, never thought about that. Unfortunately, it gives the same result as Arnaud's code, i.e. the square is too small if I overlay the line. Or did I do something wrong maybe?
Matt Tearle
Matt Tearle 2011년 5월 25일
Oh, no, my bad. I hadn't read the comments on Arnaud's answer, sorry. Looks like you'll have to write your own routine to do multiple line calls, as Sean suggests.

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


Yusuf Yilmaz
Yusuf Yilmaz 2021년 11월 10일
Hi,
It is possible to put different line style on each line in contourf command or not If so, how ?
Thanks in advance

카테고리

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