フィルターのクリア

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 Center および File ExchangeModel Building and Assessment についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by