How to change the color of different parts of regression plots using fitlm function?

조회 수: 90 (최근 30일)
I used fitlm function to calculate the linear regression and plot the outcomes, but the problem is I can't change the color of the regression or the confidence interval lines, the only thing I can change is the color of my data in the plot. Is there anyway to change the color of the regression and confidence interval lines?
here's my code
tbl = table(delayTime, reactionTime);
regressionPlot = fitlm(tbl, 'linear');
plot(regressionPlot, 'color', 'b')
  댓글 수: 4
Bill Dimakopoulos
Bill Dimakopoulos 2021년 2월 3일
This is true but h is a line array and simply one could select some elements of this array :
For instance:
set(h(3),'Color', 'b') --> changes the lower confidence bound color to blue
set(h(4),'Color','k') --> changes the upper confidence bound color to black
Similarly for the data points (h(1)) and for the fitted line (h(2)).
Adam Danz
Adam Danz 2021년 2월 3일
Yes, that's a better approach. It's only weakness is that it assumes the order of objects in the handle array. Alternatively, those handles can be identified by their legend strings which is slightly better but those legend strings could change in future releases as well.

댓글을 달려면 로그인하십시오.

답변 (1개)

Adam Danz
Adam Danz 2021년 2월 2일
plot(mdl) returns a plot of the fitml model and includes a legend that shows the names of each object.
The most robust way edit the properties of each component is to use findobj along with the legend strings to isolate the handle for each object. This method is robus because it does not rely on the order of the handle which could change in future releases or under different contexts.
Demo:
% Plot the linear model
T = table((0:50)', linspace(50,1000,51)'+rand(51,1)*500, 'VariableNames', {'X','Y'});
mdl = fitlm(T, 'linear');
h = plot(mdl)
Notice the legend strings match the search queries below.
% Get handles to plot components
dataHandle = findobj(h,'DisplayName','Data');
fitHandle = findobj(h,'DisplayName','Fit');
% The confidence bounds have 2 handles but only one of
% the handles contains the legend string. The first
% line below finds that object and then searches for
% other objects in the plot that have the same linestyle
% and color.
cbHandles = findobj(h,'DisplayName','Confidence bounds');
cbHandles = findobj(h,'LineStyle',cbHandles.LineStyle, 'Color', cbHandles.Color);
Now you can change the graphics properties of each item
dataHandle.Color = 'k';
fitHandle.Color = [1 .6445 0]; %orange
set(cbHandles, 'Color', 'b', 'LineWidth', 1)

카테고리

Help CenterFile Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by