how to make the regression line of lsline function colored

26 views (last 30 days)
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 Comment
Joris Bockhofer
Joris Bockhofer on 6 Jul 2023
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!

Sign in to comment.

Accepted Answer

Oleg Komarov
Oleg Komarov on 28 Feb 2011
When you add the regression line call it like this:
h = lsline;
set(h(1),'color','r')
set(h(2),...
Oleg
  3 Comments
Honey
Honey on 16 Dec 2021
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 on 6 Jul 2023
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

Sign in to comment.

More Answers (0)

Categories

Find more on Data Distribution Plots 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!