how to make the regression line of lsline function colored

조회 수: 31 (최근 30일)
Jonas
Jonas 2011년 2월 28일
댓글: Joris Bockhofer 2023년 7월 6일
I have 3 scatter plots that are diplayed in the same graph in three different colors. I would like each of them to have a regression line of the same color. I can only get three grey regression line with the lsline command.
  댓글 수: 1
Joris Bockhofer
Joris Bockhofer 2023년 7월 6일
My recommendation would be to forget about lsline
and simply calculate the linear regression yourself and plot the result separately with the color of the original plot.
lsline has taken way more time out of my life than it should and i hope this finds anyone having trouble with coloring it.
My approach looks like this:
rng default % For reproducibility
x = 1:10;
y1 = x + randn(1,10);
y2 = 2*x + randn(1,10); % same size as x
figure
ax1 = gca;
hold(ax1, 'on');
xData = [x; x];
yData = [y1;y2];
legendStrArray = string(); % creat a string array to hold label information
for i = 1:numel(xData(:,1))
dataX = [xData(i,:)];
dataY = [yData(i,:)];
pl1 = scatter(ax1, dataX, dataY); % plot original data
colorRGB = pl1.CData; % get color of plot
% Fit a linear model
model = fitlm(dataX, dataY);
% Get the fitted line data
xFit = linspace(min(dataX), max(dataX), 100);
yFit = feval(model, xFit);
h = plot(ax1, xFit, yFit, 'Color', colorRGB); % plot regression with color
% add to legend with end+1 strategy for when you dont want to set up a dedicated counter
legendStrArray(1,end+1) = "Datasheet: " + string(i); % legend for data
legendStrArray(1,end+1) = "Datasheet: " + string(i) + ", Regression"; % legend for it's regression
% otherwise it mess up the number of plots vs the number of legend entries
end
legendStrArray = legendStrArray(1,2:end);% take off the first empty entry from the legend array bc of end+1
legend(ax1,legendStrArray, 'Location', 'best'); % have a working legend
hold(ax1, 'off');
Why complicated when it could be simple (or was it the other way around?) - Hope this saves you the headache!

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

채택된 답변

Oleg Komarov
Oleg Komarov 2011년 2월 28일
When you add the regression line call it like this:
h = lsline;
set(h(1),'color','r')
set(h(2),...
Oleg
  댓글 수: 3
Honey
Honey 2021년 12월 16일
I have a problem. I have loop for my scattres and I want to do the same which Jonas asked. But the color doesn't change and it is the same color of the last one in the loop.
Joris Bockhofer
Joris Bockhofer 2023년 7월 6일
Honestly this should be made way more obvious, i spend way too long just trying to set color because the way in the documentation wasnt working so i had to do : [h1.Color] = deal(color); because it kept giving me assignment errors, and then i had to find out that lsline automatically plots a line for each held scatterplot, so setting the color from inside the same loop as you are plotting the scatterplot doesnt seem possible. you'd have to create a second loop for iterating over the handles.... this is just top notch

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by