Plot contours from coordinates as a line

조회 수: 8 (최근 30일)
Guillaume Le Goc
Guillaume Le Goc 2020년 5월 18일
댓글: darova 2020년 5월 18일
Hey there, I'm struggling with a problem regarding plotting a shape directly from x,y coordinates.
I have a set of (x,y) coordinates of points that describes several contours in the (x, y) plane.
I can display those contours using :
scatter(x, y)
The problem using this function is that the figure is quite heavy, and exporting it to pdf for, say, editing it on Inkscape results in a vector image with a weight of more than 10MB that Inkscape has troubles handling. Note that the coordinates contain more than 2000 (x,y) pairs.
The solution would be to plot those contours as a line rather with discrete points, but I don't find a way.
The two issues I get that make impossible to use plot rather than scatter is that :
  • coordinates are in unordered manner so it gives me multiple lines going everywhere
  • coordinates reprensent multiple contours at once so the different regions are connected with a line at some point.
I guess I could achieve something with the contour or surf functions, setting a fake z dimension that would describe the contours but I didn't manage...
Any insights? You'll find attached an example of two objects whose contours are described by their x,y coordinates.
Cheers,
Guillaume
  댓글 수: 2
KSSV
KSSV 2020년 5월 18일
Try boundary function.
Guillaume Le Goc
Guillaume Le Goc 2020년 5월 18일
I didn't know this function, it could have worked but it simplifiestoo much some complex shape and doesn't really work when there is two objects.

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

답변 (1개)

darova
darova 2020년 5월 18일
Solution
  • separate each data using logical indexing
  • order data using angle
clc,clear
s = load('xycontours.txt');
x = s(:,1);
y = s(:,2);
ix = x < 0.25;
x1 = x(ix); % left points
y1 = y(ix);
x2 = x(~ix); % right points
y2 = y(~ix);
% center each data set and convert to polar (calculate angle)
[t1,r1] = cart2pol(x1-mean(x1),y1-mean(y1));
[t2,r2] = cart2pol(x2-mean(x2),y2-mean(y2));
[~,ix1] = sort(t1); % sort angle
[~,ix2] = sort(t2); % sort angle
plot(x1(ix1),y1(ix1),'r')
line(x2(ix2),y2(ix2))
  댓글 수: 2
Guillaume Le Goc
Guillaume Le Goc 2020년 5월 18일
That's clever, nice one!
But unfortunately I have several sets of coordinates such as this one, and I wanted an unsupervised way (ie. with no knowledge of number of regions, where to threshold...). I guess it's feasible to build something that automates this but I don't really have time and it's kinda okay the way it is with scatter. I thought maybe MATLAB could do this "natively".
darova
darova 2020년 5월 18일
Do you know how many datasets you have? Maybe clusterdata
s = load('xycontours.txt');
x = s(:,1);
y = s(:,2);
ix = clusterdata(s,'maxclust',2); % number of data sets
for i = 1:numel(unique(ix))
x1 = x(ix==i); % choose dataset
y1 = y(ix==i);
[t1,r1] = cart2pol(x1-mean(x1),y1-mean(y1));
[~,ix1] = sort(t1); % sort angle
line(x1(ix1),y1(ix1))
end

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

카테고리

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