フィルターのクリア

How do I connect points in a scatter plot with a line?

934 ビュー (過去 30 日間)
Matt
Matt 2014 年 7 月 15 日
編集済み: MathWorks Support Team 2018 年 11 月 27 日
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

採用された回答

Ben11
Ben11 2014 年 7 月 15 日
編集済み: MathWorks Support Team 2018 年 11 月 27 日
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 件のコメント
Matt
Matt 2014 年 7 月 15 日
Thanks! It works :)
Ben11
Ben11 2014 年 7 月 15 日
Great! You can accept John's answer as I kind of use his to answer your question. Glad it helped!

サインインしてコメントする。

その他の回答 (2 件)

John D'Errico
John D'Errico 2014 年 7 月 15 日
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 件のコメント
H ZETT M
H ZETT M 2017 年 1 月 18 日
編集済み: H ZETT M 2017 年 1 月 18 日
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
John D'Errico 2017 年 1 月 18 日
@ZETT: This is a TOTALLY different question. Ask it as a question. A separate and new question.

サインインしてコメントする。


Brian B
Brian B 2014 年 7 月 15 日
編集済み: Brian B 2014 年 7 月 15 日
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 件のコメント
John D'Errico
John D'Errico 2014 年 7 月 15 日
Both of you have missed that for some reason, he wants to plot the first 8 elements.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeScatter Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by