Connect points in two added variable plots of linear regressions
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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!
0 Kommentare
Akzeptierte Antwort
Voss
am 18 Apr. 2022
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
hold on;
h2 = plotAdded(mdlpost,2) % store the lines as h2
% 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 Kommentare
Weitere Antworten (0)
Siehe auch
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!