Changing Plot colors while using fitlm

10 views (last 30 days)
ryan
ryan on 7 Jan 2025 at 20:24
Answered: Voss on 7 Jan 2025 at 21:02
I'm trying to plot 4 different sets of data with a linear regression for each. I used the fitlm command to find hte linear regression. My problem arises with the plot all being the same color for each set of data, blue for the points and red for the regression line. I need them to be different colors for each data set for clarity sake.
data=readmatrix('straingage_caldata.csv');
c1F=data(:,1);
c1V=data(:,2);
c3F=data(:,3);
c3V=data(:,4);
c4F=data(:,5);
c4V=data(:,6);
c5F=data(:,7);
c5V=data(:,8);
c1LR=fitlm(c1F,c1V)
c3LR=fitlm(c3F,c3V)
c4LR=fitlm(c4F,c4V)
c5LR=fitlm(c5F,c5V)
figure
hold on
plot(c1LR,"r");
plot(c3LR);
plot(c4LR);
plot(c5LR);
legend('Crane 1','Crane 1 Lin Reg','Crane 3','Crane 3 Lin Reg','Crane 4','Crane 4 Lin Reg','Crane 5','Crane 5 Lin Reg',"Location","SouthEast")
thanks

Answers (2)

Voss
Voss on 7 Jan 2025 at 21:02
LinearModel.plot() does not allow you to specify line properties, but you can modify them after they are plotted.
Here's an example of that, using made-up data:
% data=readmatrix('straingage_caldata.csv');
data = (1:25).'+10*[0 1 0 2 0 3 0 4]+randn(25,8)
data = 25×8
0.3630 10.9753 1.6793 20.3631 2.5345 30.3200 -0.2674 39.9662 2.2548 12.9764 2.6190 22.2428 1.8456 31.5432 3.5648 42.0293 3.0671 13.8548 3.0756 23.1554 4.4185 32.4425 2.6897 42.8468 3.6592 12.5660 2.2575 23.0667 3.5774 36.4245 3.9321 43.4142 3.9784 15.2027 4.6168 25.3255 3.8259 37.1344 4.3576 46.1327 5.6460 17.1276 6.2181 25.9225 7.6325 36.5382 5.9802 45.6946 5.8379 18.0347 6.9164 28.2709 5.7577 37.7837 8.2573 48.3871 6.2705 17.5095 7.5226 28.7899 9.3443 38.2760 7.1833 47.9630 9.1545 20.5494 9.6614 29.2997 8.4752 39.1659 7.4979 50.9276 7.6650 19.9289 10.1541 29.7842 8.5245 40.1251 10.9534 47.9336
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
c1F=data(:,1);
c1V=data(:,2);
c3F=data(:,3);
c3V=data(:,4);
c4F=data(:,5);
c4V=data(:,6);
c5F=data(:,7);
c5V=data(:,8);
c1LR=fitlm(c1F,c1V);
c3LR=fitlm(c3F,c3V);
c4LR=fitlm(c4F,c4V);
c5LR=fitlm(c5F,c5V);
figure
hold on
h = plot(c1LR) % Note that each plo() makes 3 line objects
h =
3x1 Line array: Line (Data) Line (Fit) Line (95% conf. bounds)
h(1).Color = 'r'; % red
h(1).Marker = 'o'; % circle
h(2).Color = 'm'; % magenta
delete(h(3)) % delete the 95% confidence bounds lines, since you're not including them in the legend
h = plot(c3LR);
h(1).Color = 'b'; % blue
h(1).Marker = 'diamond';
h(2).Color = 'c'; % cyan
delete(h(3))
h = plot(c4LR);
h(1).Color = 'k'; % black
h(1).Marker = 'square';
h(2).Color = [0.75 0.75 0.75]; % grey
delete(h(3))
h = plot(c5LR);
h(1).Color = [0 0.5 0]; % dark green
h(1).Marker = 'hexagram';
h(2).Color = 'g'; % green
delete(h(3))
legend('Crane 1','Crane 1 Lin Reg','Crane 3','Crane 3 Lin Reg','Crane 4','Crane 4 Lin Reg','Crane 5','Crane 5 Lin Reg',"Location","SouthEast")
Another option is to evaluate the LinearModel fit objects and plot those values using the base plot function, which of course gives you more control at plotting time.
Here's an example of that, using the same data:
figure
hold on
c1V_estimate = feval(c1LR,c1F);
plot(c1F,c1V,'ro')
plot(c1F,c1V_estimate,'m')
c3V_estimate = feval(c3LR,c3F);
plot(c3F,c3V,'bd')
plot(c3F,c3V_estimate,'c')
c4V_estimate = feval(c4LR,c4F);
plot(c4F,c4V,'ks')
plot(c4F,c4V_estimate,'Color',[0.75 0.75 0.75])
c5V_estimate = feval(c5LR,c5F);
plot(c5F,c5V,'h','MarkerEdgeColor',[0 0.5 0])
plot(c5F,c5V_estimate,'g')
legend('Crane 1','Crane 1 Lin Reg','Crane 3','Crane 3 Lin Reg','Crane 4','Crane 4 Lin Reg','Crane 5','Crane 5 Lin Reg',"Location","SouthEast")

Star Strider
Star Strider on 7 Jan 2025 at 20:59
I’m not sure how this works in R2019a, however if you don’t specify any specific colours in the plot calls, each plot has different (and corresponding) colours for the data and the confidence limits.
Example —
data = randn(10,8); % Create ‘data’
data(:,2:2:end) = data(:,2:2:end) .* randn(1,4) + randn(1,4); % Create ‘data’
data(:,1:2:end) = sort(data(:,1:2:end)) % Create ‘data’
data = 10×8
-2.2283 1.3033 -1.4719 0.3537 -1.1857 -1.3363 -0.7467 -0.0810 -1.9108 1.0429 -1.3244 2.4812 -0.7383 -1.3659 -0.6007 -0.0688 -1.6657 0.2314 -0.5899 2.2737 -0.7300 2.3860 -0.3330 -0.0634 -0.2456 0.1488 -0.4961 2.8662 -0.7257 -1.4343 -0.2680 -0.0720 -0.1447 -0.1372 -0.1405 3.9339 -0.4042 -1.4226 0.3125 -0.0644 -0.0812 -0.2726 -0.0471 2.9159 -0.2593 -0.6666 0.5996 -0.0546 -0.0811 -0.2552 0.1669 0.4803 -0.0939 -1.0705 0.6958 -0.0649 -0.0730 -0.2565 0.2769 2.5588 0.5819 1.2705 0.9101 -0.0766 1.1337 1.7531 0.3528 0.8566 0.9799 1.4666 0.9386 -0.0722 1.3652 -0.5166 1.0038 -0.4142 1.6976 -0.6381 1.5472 -0.0750
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
c1F=data(:,1);
c1V=data(:,2);
c3F=data(:,3);
c3V=data(:,4);
c4F=data(:,5);
c4V=data(:,6);
c5F=data(:,7);
c5V=data(:,8);
c1LR=fitlm(c1F,c1V)
c1LR =
Linear regression model: y ~ 1 + x1 Estimated Coefficients: Estimate SE tStat pValue ________ _______ _______ _______ (Intercept) 0.22129 0.26221 0.84395 0.42321 x1 -0.21073 0.21671 -0.9724 0.35933 Number of observations: 10, Error degrees of freedom: 8 Root Mean Squared Error: 0.784 R-squared: 0.106, Adjusted R-Squared: -0.00609 F-statistic vs. constant model: 0.946, p-value = 0.359
c3LR=fitlm(c3F,c3V)
c3LR =
Linear regression model: y ~ 1 + x1 Estimated Coefficients: Estimate SE tStat pValue ________ _______ ________ ________ (Intercept) 1.7257 0.47865 3.6054 0.006928 x1 -0.46222 0.62997 -0.73372 0.48406 Number of observations: 10, Error degrees of freedom: 8 Root Mean Squared Error: 1.44 R-squared: 0.0631, Adjusted R-Squared: -0.0541 F-statistic vs. constant model: 0.538, p-value = 0.484
c4LR=fitlm(c4F,c4V)
c4LR =
Linear regression model: y ~ 1 + x1 Estimated Coefficients: Estimate SE tStat pValue ________ _______ ________ _______ (Intercept) -0.2383 0.45857 -0.51966 0.61737 x1 0.48775 0.53198 0.91687 0.38602 Number of observations: 10, Error degrees of freedom: 8 Root Mean Squared Error: 1.44 R-squared: 0.0951, Adjusted R-Squared: -0.018 F-statistic vs. constant model: 0.841, p-value = 0.386
c5LR=fitlm(c5F,c5V)
c5LR =
Linear regression model: y ~ 1 + x1 Estimated Coefficients: Estimate SE tStat pValue __________ _________ _______ _________ (Intercept) -0.069409 0.0028001 -24.788 7.499e-09 x1 0.00045743 0.0035718 0.12806 0.90126 Number of observations: 10, Error degrees of freedom: 8 Root Mean Squared Error: 0.00815 R-squared: 0.00205, Adjusted R-Squared: -0.123 F-statistic vs. constant model: 0.0164, p-value = 0.901
figure
hold on
plot(c1LR);
plot(c3LR);
plot(c4LR);
plot(c5LR);
legend('Crane 1','Crane 1 Lin Reg','Crane 3','Crane 3 Lin Reg','Crane 4','Crane 4 Lin Reg','Crane 5','Crane 5 Lin Reg',"Location","SouthEast")
.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!