How do I join the points of a scatter plot to make a line?

5 visualizzazioni (ultimi 30 giorni)
I have a scatter plot as given below. Each point comes from a xy pair (for example -
670 358
686 363
1677 365
681 369
1680 376
724 377
How do i join these points to make a line?

Risposte (4)

Matt J
Matt J il 23 Mag 2016
Modificato: Matt J il 23 Mag 2016
Use PLOT instead of SCATTER. Connecting with lines defeats the purpose of a scatter plot.
  8 Commenti
Matt J
Matt J il 23 Mag 2016
I think you mean you are trying to find a best fit line to the points.

Accedi per commentare.


Walter Roberson
Walter Roberson il 24 Mag 2016
If you have R2014b or later, use boundary to get the outline of the implied shape. If you are using an earlier release you can look in the File Exchange for Alpha Shapes routines.
"following the same shape as the points" is not well defined when there are multiple points with the same X or same Y coordinate.
  2 Commenti
Coder Bee
Coder Bee il 24 Mag 2016
I use R2014a. Alpha shapes doesn't seem to be working well for me. Can you help me with an approach of replicating a hadn-drawn line on matlab?
Walter Roberson
Walter Roberson il 24 Mag 2016
You have not presented a hand-drawn line that we could examine the properties of. We need to observe the decision logic of how to connect points that are close together.

Accedi per commentare.


Image Analyst
Image Analyst il 24 Mag 2016
See attached demo for polyfit. It will show you how to fit a line through your points.
  2 Commenti
Matt J
Matt J il 24 Mag 2016
polyfit is not the best choice because both x and y have measurement noise in them.
Image Analyst
Image Analyst il 24 Mag 2016
I bet it would do a respectable job though. Maybe not the theoretical best, but usable for his purposes. Perhaps if he uploaded the data we could try it.

Accedi per commentare.


Matt J
Matt J il 24 Mag 2016
Modificato: Matt J il 24 Mag 2016
Here's a total least squares fit,
%Get line equation
xymean=mean(xy);
[~,~,V]=svd( bsxfun(@minus,xy,xymean) ,0);
e=V(:,end);
e=[e;-dot(e,xymean)];
fun2=@(x,y) e(1)*x+e(2)*y +e(3);
%Plot
scatter(xy(:,1), xy(:,2) ); hold on
xymin=min(xy);
xymax=max(xy);
xb=[xymin(1), xymax(1)];
yb=[xymin(2), xymax(2)];
ezplot(fun2,[xb,yb]); hold off
  3 Commenti
Walter Roberson
Walter Roberson il 24 Mag 2016
In the center part of this sample, there is an area in which there is a loop. I cannot see any justification for a loop being there based upon the original image. I also cannot justify the large gap in the line being where you show it -- not unless there are additional gaps at least as large.
Matt J
Matt J il 25 Mag 2016
Modificato: Matt J il 25 Mag 2016
@Coder,
For the sake of clear communication, you need to distinguish between lines and curves. The thing you've drawn is a very wavy curve, not a line. A line, by definition, is perfectly straight.
Nor does it seem to be a "fit" as you said you wanted earlier. It looks like the exact drawing that you started with. In that case, why not just use the original image that you are given for whatever it is that you're doing?
If you're trying to reconstruct the exact path followed by the pen, you won't be able to without further information and constraints. There is no unique path that connects a given discrete set of points.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by