Plotting multiple linear models on one plot
18 views (last 30 days)
Show older comments
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,
0 Comments
Accepted Answer
Voss
on 1 Apr 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'});
0 Comments
More Answers (0)
See Also
Categories
Find more on Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!