Changing Plot colors while using fitlm
10 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
回答 (2 件)
Voss
2025 年 1 月 7 日
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)
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(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")
0 件のコメント
Star Strider
2025 年 1 月 7 日
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’
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);
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")
.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Geodesy and Mapping についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!