How do I join the points of a scatter plot to make a line?
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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?
0 commentaires
Réponses (4)
Walter Roberson
le 24 Mai 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 commentaires
Walter Roberson
le 24 Mai 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.
Image Analyst
le 24 Mai 2016
See attached demo for polyfit. It will show you how to fit a line through your points.
2 commentaires
Matt J
le 24 Mai 2016
polyfit is not the best choice because both x and y have measurement noise in them.
Image Analyst
le 24 Mai 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.
Matt J
le 24 Mai 2016
Modifié(e) : Matt J
le 24 Mai 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 commentaires
Walter Roberson
le 24 Mai 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
le 25 Mai 2016
Modifié(e) : Matt J
le 25 Mai 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.
Voir également
Catégories
En savoir plus sur Scatter Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!