Plotting multiple linear models on one plot

25 vues (au cours des 30 derniers jours)
Khalil EL HACHEM
Khalil EL HACHEM le 1 Avr 2022
Réponse apportée : Voss le 1 Avr 2022
Hi all,
I have a dataset and I am fitting multiple linear models to it. The first model is fit to the first 1/3 section of the data, the second model is fit to the first 2/3 section of the data, the third model is fit to the last 2/3 section of the data, the fourth model is fit to the last 2/3 section of the data and the last model is fit to all data.
I am using the fitlm fucntion to generate these linear models and the numbers of the mdl=fitlm() function match what I am seeing in excel (error <1%). However, when I try to visualize all the data on one plot I am getting all the linear models in red. So it is very hard to differentiate between one model or the other. Any idea how to change the color of each linear plot?
Thanks,

Réponse acceptée

Voss
Voss le 1 Avr 2022
You can store the line handles returned from plot(), and change the colors after the lines are plotted.
mdl1 = fitlm(rand(5,1),rand(5,1));
h1 = plot(mdl1); % h1 contains the lines for model 1
set(h1(1),'Color','g'); % make data line for model 1 green
set(h1(2:end),'Color','c'); % make fit and confidence lines for model 1 cyan
mdl2 = fitlm(rand(5,1),rand(5,1));
hold on
h2 = plot(mdl2); % h2 contains the lines for model 2
set(h2(1),'Color',[1 0.5 0]); % make data line for model 2 orange
set(h2(2:end),'Color','m'); % make fit and confidence lines for model 2 magenta
legend([h1(1:3); h2(1:3)],{'Data 1','Fit 1','Confidence 1','Data 2','Fit 2','Confidence 2'});

Plus de réponses (0)

Catégories

En savoir plus sur Linear and Nonlinear Regression dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by