Changing fit line style changes the fit line (fit line shape)

52 views (last 30 days)
Milos Krsmanovic
Milos Krsmanovic on 8 Jul 2021
Edited: dpb on 8 Jul 2021
Hi all,
I have a scatter plot fit line for which I'm able to successfully change the color but I'm struggling to change the line style from solid to dashed.
The code I start with plots a nice, solid line in the color I want:
f=fit(fib_flow_semi.As, fib_flow_semi.Np,'poly1');
fit_semi = plot(f,fib_flow_semi.As, fib_flow_semi.Np);
set(fit_semi,'Color',c_semi);
Now, if I try to change the line so it's a dashed line:
f=fit(fib_flow_semi.As, fib_flow_semi.Np,'poly1');
fit_semi = plot(f,fib_flow_semi.As, fib_flow_semi.Np);
set(fit_semi,'Color',c_semi,'Linestyle','--');
I get:
What I would like to see is the line shaped as shown on the first image, but dashed as shown on the second one.
I put my code together reading examples here but obviously I'm missing something. I checked the line spec but I'm unable to figure it out on my own why changing the line style includes additional data subsets and plots these smaller "sub-fit" lines.
Please can someone give me some ideas on what I'm doing wrong?
Thank you.

Accepted Answer

Cris LaPierre
Cris LaPierre on 8 Jul 2021
Edited: Cris LaPierre on 8 Jul 2021
It looks like you have set the line style for the solid blue data points as well as the fit line.
% Default view
x = (1:10)';
y = 0.4*x + rand(10,1)*10;
f=fit(x, y,'poly1');
plot(f,x, y)
% Setting format of all line objects
figure
fit_semi = plot(f,x, y);
set(fit_semi,'Linestyle','--');
Note that fit_semi has 2 line objects in it here. When you don't specify a specific line object, the formatting applies to all of them. If you just want to just change the format of the fit line, specify it as fit_semi(2).
figure
fit_semi = plot(f,x, y);
set(fit_semi(2),'Linestyle','--');
  4 Comments
Milos Krsmanovic
Milos Krsmanovic on 8 Jul 2021
Thank you for your answer. You were spot on on the intent, I have multiple data sets and multiple fit lines, hence the need to differentiate them by style and color. Introducing the argument fit_semi(2) solves the issue. Formatting of the line does affect the data points but I solved that by moving the fit line formatting upstream of the scatter plotting command. I appreciate your help.

Sign in to comment.

More Answers (1)

dpb
dpb on 8 Jul 2021
Edited: dpb on 8 Jul 2021
The overloaded plot functions for fit and cfit objects are replete with added functionality -- hard to wade through all the possibilities.
Don't have your data to be able to duplicate, and rushed for time to try to replicate, but try using the FitLineSpec positional argument in the plot() call --
fit_semi = plot(f,'--',fib_flow_semi.As, fib_flow_semi.Np);
set(fit_semi,'Color',c_semi);
and see if that helps any.
  4 Comments
dpb
dpb on 8 Jul 2021
No problem; I understood that; I was just commenting that to figure out why the syntax didn't produce the expected result would require knowing the details of how the fit object was created as well as the plot since clearly things more than just the one line are happening.
If you have a working solution, it may not even matter to try to diagnose the root cause; my other point in the last was to figure out for myself as well as for the forum the way to not have a "magic number" buried in the code that makes it dependent on nothing in the internals changing.
And, as I noted above, I've not used the fit object all that much yet; there's still a lot about it I don't yet know so it's a chance to poke at the edges a little more...

Sign in to comment.

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!