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

79 次查看(过去 30 天)
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.

采纳的回答

Cris LaPierre
Cris LaPierre 2021-7-8
编辑:Cris LaPierre 2021-7-8
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 个评论
Cris LaPierre
Cris LaPierre 2021-7-8
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.
Milos Krsmanovic
Milos Krsmanovic 2021-7-8
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.

请先登录,再进行评论。

更多回答(1 个)

dpb
dpb 2021-7-8
编辑:dpb 2021-7-8
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 个评论
Milos Krsmanovic
Milos Krsmanovic 2021-7-8
@dpb, my previous reply to you is missing - not sure what happened. What I explained was that if I set linestyle within the plot function as you proposed I get the same funky line as shown on my figure 2. Regarding your second comment, please forgive me for not sharing the whole code, I'm still putting everything together but yes, I am plotting multiple things. I read a collated csv file into a MATLAB table, then match strings in multiple columns via strcmp to filter values from particular rows. This is how I filter multiple data sets from one master data file. I plot each set separately and consecutively. I made this whole thing work by adjusting the order of plotting, by setting the parameter in the brackets fit_semi(2) to specify which element the set command applies to. Thank you for replying to me, I appreciate your inputs and the time.
dpb
dpb 2021-7-8
编辑:dpb 2021-7-8
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...

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Preprocessing 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by