Plot contours from coordinates as a line

6 views (last 30 days)
Guillaume Le Goc
Guillaume Le Goc on 18 May 2020
Commented: darova on 18 May 2020
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 Comments
Guillaume Le Goc
Guillaume Le Goc on 18 May 2020
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.

Sign in to comment.

Answers (1)

darova
darova on 18 May 2020
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 Comments
Guillaume Le Goc
Guillaume Le Goc on 18 May 2020
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 on 18 May 2020
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

Sign in to comment.

Categories

Find more on Contour Plots in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!