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

조회 수: 4 (최근 30일)
Coder Bee
Coder Bee 2016년 5월 23일
편집: Matt J 2016년 5월 25일
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.
Coder Bee
Coder Bee 2016년 5월 24일
Yes, can you help me?

댓글을 달려면 로그인하십시오.


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?
Walter Roberson
Walter Roberson 2016년 5월 24일
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
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