Borrar filtros
Borrar filtros

Connect points in two added variable plots of linear regressions

2 visualizaciones (últimos 30 días)
bsriv
bsriv el 18 de Abr. de 2022
Comentada: Voss el 18 de Abr. de 2022
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!

Respuesta aceptada

Voss
Voss el 18 de Abr. de 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
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 comentarios
Voss
Voss el 18 de Abr. de 2022
(@bsriv Answer moved to comment:)
Beautiful thank you!
Voss
Voss el 18 de Abr. de 2022
You're welcome! If that works, please click 'Accept this Answer'. Thanks!

Iniciar sesión para comentar.

Más respuestas (0)

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by