Plot contours from coordinates as a line
조회 수: 8 (최근 30일)
이전 댓글 표시
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
답변 (1개)
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
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 Center 및 File Exchange에서 Contour Plots에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!