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

5 次查看(过去 30 天)
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?

回答(4 个)

Matt J
Matt J 2016-5-23
编辑:Matt J 2016-5-23
Use PLOT instead of SCATTER. Connecting with lines defeats the purpose of a scatter plot.
  8 个评论
Matt J
Matt J 2016-5-23
I think you mean you are trying to find a best fit line to the points.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2016-5-24
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 个评论
Coder Bee
Coder Bee 2016-5-24
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?

请先登录,再进行评论。


Image Analyst
Image Analyst 2016-5-24
See attached demo for polyfit. It will show you how to fit a line through your points.
  2 个评论
Matt J
Matt J 2016-5-24
polyfit is not the best choice because both x and y have measurement noise in them.
Image Analyst
Image Analyst 2016-5-24
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
Matt J 2016-5-24
编辑:Matt J 2016-5-24
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 个评论
Walter Roberson
Walter Roberson 2016-5-24
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 2016-5-25
编辑:Matt J 2016-5-25
@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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Scatter Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by