Changing fit line style changes the fit line (fit line shape)
85 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
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
Cris LaPierre
on 8 Jul 2021
We don't know how the original figure was created, but it doesn't appear to me like the fit line in the original post is fitting all the data. I'd expect a differnt slope if it were. It appears to only be fitting the solid blue data points.
Perhaps the figure was created using multiple plot commands and hold. The fit and final plot command only added the solid blue points and line.
More Answers (1)
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
on 8 Jul 2021
Edited: 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...
See Also
Categories
Find more on Data Preprocessing 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!