Clear Filters
Clear Filters

How can I set two different color for each lsline in the scatter plot?

16 views (last 30 days)
I have one question about lsline.
Basically, I want to have two lsline(C and D), one for A and one for B respectively.
I would like to set different color for each lsline and also would like to make legend for each one.
So I wrote the code as below. However, it gives me the two blue lsline instead of one read and one blue.
So I was wondering how should I make the code to get the right figure.
Thanks in advance.
% Subplot
A = scatter(semi2008(h_to_plot),semi2001(h_to_plot),[],[0 0.4470 0.7410],'o','Linewidth',1);
hold on
C = lsline;
hold on
B = scatter(semi2008_zoomed(h_to_plot_zoomed),semi2001(h_to_plot_zoomed),[],[0.8500 0.3250 0.0980],'o','Linewidth',1);
hold on
D = lsline;
hold on
grid on
legend([A B C D],'scatter1','scatter2','line1','line2','Location','southeast')

Accepted Answer

Walter Roberson
Walter Roberson on 24 Jan 2022
lsline creates a least squared line for each scatter plot in the axes.
At the time you call lsline the first time you have plotted only A so C reflects only A.
At the time you call lsline the second time, you have A and B plotted, and lsline creates lines for each of them, so D contains the handles for two lines.
When you put it all together you have a vector with five handles: one for A directly, one for B directly, one the first lsline for A, then one for the second lsline for A, then one for the lsline for B.
What you should do is get rid of the first lsline call, since the second lsline call generates lines for each of the scatter plots.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!