How to plot multiple scatter plot with regression line in one plot
12 views (last 30 days)
Show older comments
I am trying to show multiple scatter plots with regression line in one plot with the hold on function. But I get the error message
"Assigning to 2 elements using a simple assignment statement is not supported. Consider using comma-separated list
assignment." What do I do wrong?
scatter(all_subs_data(:,4),all_subs_data(:,144),"filled","o","MarkerFaceColor","r","SizeData",100, 'MarkerFaceAlpha',.1)
h1 = lsline
h1.Color = 'r'
h1.LineWidth = 3.0000
hold on
scatter(all_subs_data(:,5),all_subs_data(:,143),"filled","o","MarkerFaceColor","b","SizeData",100, 'MarkerFaceAlpha',.1)
h2 = lsline
h2.Color = 'b'
h2.LineWidth = 3.0000
hold off
0 Comments
Accepted Answer
Voss
on 2 Nov 2022
Note that "lsline superimposes a least-squares line on each scatter plot in the current axes".
That is, the second call to lsline returns two lines, since there are two scatter plots in the axes at that point in time.
To avoid creating redundant lines, make both scatter plots first, call lsline, and then set the properties of each line object returned:
all_subs_data = rand(10,150); % random data
scatter(all_subs_data(:,4),all_subs_data(:,144),"filled","o","MarkerFaceColor","r","SizeData",100, 'MarkerFaceAlpha',.1)
hold on
scatter(all_subs_data(:,5),all_subs_data(:,143),"filled","o","MarkerFaceColor","b","SizeData",100, 'MarkerFaceAlpha',.1)
h = lsline();
h(1).Color = 'r';
h(1).LineWidth = 3.0000;
h(2).Color = 'b';
h(2).LineWidth = 3.0000;
hold off
More Answers (0)
See Also
Categories
Find more on Scatter 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!