connecting lines between adjacent scatter plots

조회 수: 22 (최근 30일)
Steve
Steve 2022년 5월 27일
댓글: Steve 2022년 5월 28일
I have four columns of data (X1, X2, Y1, Y2) each with the same number of rows (30). I want to create two scatter plots on the same graph (X1 vs Y1, blue) and (X2 vs Y2, red). Then I want a line that connects connects each blue data point to the red data point that shares the same row... so 30 lines total, each connecting 2 datapoints. How to do this? Does it require a loop?

채택된 답변

Voss
Voss 2022년 5월 27일
"Does it require a loop?"
No.
"How to do this?"
% Your data
X1 = rand(30,1);
X2 = rand(30,1);
Y1 = rand(30,1);
Y2 = rand(30,1);
% Your existing scatter plots
scatter(X1,Y1,'b')
hold on
scatter(X2,Y2,'r')
% plotting new lines. NaNs break up the segments
N = numel(X1);
xdata = [X1 X2 NaN(N,1)].';
ydata = [Y1 Y2 NaN(N,1)].';
plot(xdata(:),ydata(:),'g')
  댓글 수: 1
Steve
Steve 2022년 5월 28일
Thank you. Fantastic. This is exactly what I was asking for.
Does this method work for adding n scatters? Say for X3 vs Y3 etc. I have ten XY sets that I've collected on different days and I want to connect them in a similar fassion. Perhaps I'll need each connecting line to have a different color and remove the markers for the points and connect them with arrows to show the transition through time.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Scatter Plots에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by