1,270 views (last 30 days)
Show older comments
Matt on 15 Jul 2014
-
-
Link
Direct link to this question
https://www.mathworks.com/matlabcentral/answers/141965-how-do-i-connect-points-in-a-scatter-plot-with-a-line
Edited: MathWorks Support Team on 27 Nov 2018
Accepted Answer: Ben11
Hi
I plot a scatter for multiple points and i want to connect them using line. Below is my code. The plot i get is only scatter, I cant connect them through a line. Any help appreciated!
x=[1,2,3,4,5,6,7,8,9];
y=[1,2,3,4,5,6,7,8,9];
hold all
for i=1:8
scatter(x(i),y(i),'X')
line(x(i),y(i))
end
Thanks
Matt
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Ben11 on 15 Jul 2014
Edited: MathWorks Support Team on 27 Nov 2018
Open in MATLAB Online
If you want to plot both markers and a line, you can use the plot function and specify a line style that includes marker symbols and a line style, such as '-x'. For example, this code plots a line with crosses at the data points.
plot(x,y,'-x')
If you are trying to plot only the first eight points, then use this code instead:
plot(x(1:8),y(1:8),'-x')
If you are plotting from a cell array, then use this code instead:
plot(cell2mat(x(1:8)),cell2mat(y(1:8)),'-x')
2 Comments Show NoneHide None
Show NoneHide None
Matt on 15 Jul 2014
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/141965-how-do-i-connect-points-in-a-scatter-plot-with-a-line#comment_225902
Thanks! It works :)
Ben11 on 15 Jul 2014
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/141965-how-do-i-connect-points-in-a-scatter-plot-with-a-line#comment_225905
Great! You can accept John's answer as I kind of use his to answer your question. Glad it helped!
Sign in to comment.
More Answers (2)
John D'Errico on 15 Jul 2014
Open in MATLAB Online
You asked this question before! In fact, you asked exactly that question, but for some reason nobody managed to give a good answer.
plot(x(1:8),y(1:8),'b-x')
There is NO need for a loop. The above single line will plot x marks at each point, and connect them with a line, all in blue.
If you prefer to plot the line in blue, and the x marks in red, this will do it:
plot(x(1:8),y(1:8),'b-',x(1:8),y(1:8),'rx')
3 Comments Show 1 older commentHide 1 older comment
Show 1 older commentHide 1 older comment
Matt on 15 Jul 2014
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/141965-how-do-i-connect-points-in-a-scatter-plot-with-a-line#comment_225895
Thank you John! For ur undersnatding!
I try to ask my question here with simple way. The actual problem I have here is a bit complex. I am ploting data from Cell. That means:-
hold all
for i=1:8
scatter(x{1,i},y{1,i},'X')
line(x{1,i},y{1,i})
end
I try like this according to your previous answer!
plot(x{1,(1:8)},y{1,(1:8)},'X')
But it says me "Too many input arguments" I have spend too much time on this. I cant get the trick!!
H ZETT M on 18 Jan 2017
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/141965-how-do-i-connect-points-in-a-scatter-plot-with-a-line#comment_421201
Edited: H ZETT M on 18 Jan 2017
Hey, this question is pretty old,but I want to know something similar. I got scattered points on a 2D surface and I want just to connect those points that are close to each other. Basically I want to say something like "connect the points that are in a range of 10m to each other" Any ideas how to do this ?
John D'Errico on 18 Jan 2017
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/141965-how-do-i-connect-points-in-a-scatter-plot-with-a-line#comment_421214
@ZETT: This is a TOTALLY different question. Ask it as a question. A separate and new question.
Sign in to comment.
Brian B on 15 Jul 2014
Edited: Brian B on 15 Jul 2014
Open in MATLAB Online
Don't use a for loop:
x=[1,2,3,4,5,6,7,8,9];
y=[1,2,3,4,5,6,7,8,9];
hold on
scatter(x,y,'X')
line(x,y)
or just
plot(x,y,'-o')
1 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
John D'Errico on 15 Jul 2014
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/141965-how-do-i-connect-points-in-a-scatter-plot-with-a-line#comment_225888
Both of you have missed that for some reason, he wants to plot the first 8 elements.
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABGraphics2-D and 3-D PlotsData Distribution PlotsScatter Plots
Find more on Scatter Plots in Help Center and File Exchange
Tags
- scatter
- line
- matlab
- plot
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office