필터 지우기
필터 지우기

Connect points in two added variable plots of linear regressions

조회 수: 2 (최근 30일)
bsriv
bsriv 2022년 4월 18일
댓글: Voss 2022년 4월 18일
Hi, I am plotting two linear regressions using plot added, and I would like to connect corresponding points with a individual lines:
figure;plotAdded(mdlpre,2);hold on; plotAdded(mdlpost,2); (picture attached).
Each blue x has a corresponding red x. How do I connect each pair with individual lines.
Thank you!

채택된 답변

Voss
Voss 2022년 4월 18일
Use the output from plotAdded, which is the plotted lines. Get the first line's (data line) XData and YData for each model, and use those to make new lines connecting the models' data points:
load carsmall
MPG = MPG(1:10);
Weight = Weight(1:10);
Year = categorical(Model_Year(1:10));
tbl = table(MPG,Weight,Year);
% first model
mdlpre = fitlm(tbl,'MPG ~ Year + Weight^2');
% second model: just modify the data a bit and run fitlm again
tbl.MPG = tbl.MPG+randn(height(tbl),1);
tbl.Weight = tbl.Weight+100;
mdlpost = fitlm(tbl,'MPG ~ Year + Weight^2');
% plotting: capture the line handles so you can
% make the connection lines later
figure;
h1 = plotAdded(mdlpre,2) % store the lines as h1
h1 =
3×1 Line array: Line (data) Line (fit) Line (95% conf. bounds)
hold on;
h2 = plotAdded(mdlpost,2) % store the lines as h2
h2 =
3×1 Line array: Line (data) Line (fit) Line
% make the connection lines:
xdata = [get(h1(1),'XData'); get(h2(1),'XData')];
ydata = [get(h1(1),'YData'); get(h2(1),'YData')];
xdata(end+1,:) = NaN;
ydata(end+1,:) = NaN;
plot(xdata(:),ydata(:),'--g','DisplayName','pre-post connection')
  댓글 수: 2
Voss
Voss 2022년 4월 18일
(@bsriv Answer moved to comment:)
Beautiful thank you!
Voss
Voss 2022년 4월 18일
You're welcome! If that works, please click 'Accept this Answer'. Thanks!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Model Building and Assessment에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by