How to plot Multiple trend-lines on one scatter plot

Hi every one,
I have a scatter plot and I need to do a second order trend line for some part of my data (just for the values less than 15 or 10 on x axis). Do you know how can I do that?

Respuestas (1)

KSSV
KSSV el 13 de Sept. de 2020
Editada: KSSV el 14 de Sept. de 2020
You can pick those respective points and fit a curve to plot the trend. Let x,y be your points.
x1 = x(x<=15) ; % pick points less than 15
y1 = y(x<=15) ;
p1 = polyfit(x1,y1,1) ; % Fit a line
figure
hold on
plot(x,y,'.k')
plot(x1,polyval(p1,x1),'r')

11 comentarios

Nicky T
Nicky T el 13 de Sept. de 2020
Editada: Nicky T el 13 de Sept. de 2020
Thank you very much. Do you know how can I connect my 2nd order trendine to the horizontal line (where y=100)? Infact I do not know what is the exact change point. I want my trand line to be connected to the horizontal line for (x>15 or whatever is the change point) please see attached
KSSV
KSSV el 13 de Sept. de 2020
Editada: KSSV el 13 de Sept. de 2020
Same thin, pick those points and fit a curve again.
x2 = x(x>15) ;
y2 = y(x>15) ;
p2 = polyfit(x2,y2,1) ;
Note: I have edited the answer, there were few typo errors.
Nicky T
Nicky T el 13 de Sept. de 2020
Please see the plot below. Do you know how can I merge all three trendlines in one trendline that covers all the areas?
KSSV
KSSV el 14 de Sept. de 2020
You have to take continuous x-data to join them. In here you have taken different values. Show the code which you tried.
I think you meant 2 in polyfit(), and p1 in polyval().
p1 = polyfit(x1,y1,2) ; % Fit a second order polynomial
figure
hold on
plot(x,y,'.k')
plot(x1,polyval(p1,x1),'r') % p1, not p
KSSV
KSSV el 14 de Sept. de 2020
@Image Analyst..yes..corrected it.
Nicky T
Nicky T el 14 de Sept. de 2020
Editada: KSSV el 14 de Sept. de 2020
here is the code, it plots fitted line or curve for each part but does not connect them:
x1 = tOut(tOut>tRet) ; % pick points for min req ventilation mode
y1 = Sodamp1(tOut>tRet);
x2 = tOut(tOut<tSupp) ; % pick points for economizing mode
y2 = Sodamp1(tOut<tSupp) ;
x3 = tOut(tOut<tRet & tOut>tSupp); % pick points for economizing+cooling mode
y3 = Sodamp1(tOut<tRet & tOut>tSupp);
p1 = polyfit(x1,y1,1) ; % Fit a line
p2 = polyfit(x2,y2,2) % Fit second order or exponential ?
p3 = polyfit(x3,y3,1) ;
figure
ylim ([0,100]);
hold on
scatter(tOut,Sodamp1,'.c')
plot(x1,polyval(p1,x1),'.k')
plot(x2,polyval(p2,x2),'.k')
plot(x3,polyval(p3,x3),'.k')
Nicky, please draw what you want in red over your scatterplot. And since you seem to have three separate trends (2 flat and one curved), why do you want their endpoints connected?
Nicky T
Nicky T el 14 de Sept. de 2020
How do I draw what I want with Matlab? It is the ideal behavior of my damper in 3 different modes. I want them to be connected as one trendline and then show them with the measured data in one graph
KSSV
KSSV el 14 de Sept. de 2020
What are values of tRet, tSupp ??
Nicky T
Nicky T el 14 de Sept. de 2020
those are temperature. I have an excel file with lots of data.

Iniciar sesión para comentar.

Categorías

Más información sobre Get Started with Curve Fitting Toolbox en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 13 de Sept. de 2020

Comentada:

el 14 de Sept. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by