How to add a line to the scatter?

Hello everybody,
I have a problem to find a code which plot the line on my scatter plot. I have upload a pictures of scatter and the example line which I have drawn manualy.
If someone can help, I would be glad.
Thank you

6 Comments

Do you have x and y coordinate of the line? Or do you want to fit a line to the scatter points?
NO I just want to fit the line on the scatter plot which will show my track as I draw in the picture.
Can you share the data points?
data points you mean x and y which i use to plot the scatter right?
yes, You can attach that as a .mat file.
Here is the some part of the data.
x axis is distance, y is load. The result will not be the same as in the pictures that I upload because it was a data for different date.

Sign in to comment.

 Accepted Answer

The data you shared is distributed vertically, so I estimated a vertical line using lsqcurvefit. You can use a similar method if you know an approximate equation that fits your model.
load('mathwork.mat');
x = @(p, y) p(1)*y+p(2);
p = lsqcurvefit(x, rand(1,2), data.Load, data.Distance);
scatter(data.Distance, data.Load)
hold on;
plot(x(p, data.Load), data.Load, 'LineWidth', 2);

4 Comments

Thanks a lot. Yes with this data it will be vertical because it is a very small part of whole. Normally, when I plot whole data as you may notice from the picture that I uploaded the graph is kind of fluctuation. Therefore do you think if I use:
x = @(p, y) p(1)*y+p(2);
p = lsqcurvefit(x, rand(1,2), data.Load, data.Distance);
I will get the fluctuated curve?
Well the code which I use is:
x = @(p, y) p(1)*y+p(2);
p = lsqcurvefit(x, rand(1,2), new3536.Load, new3536.Tunnelstation);
subplot(6,1,1);
scatter(new3536.Tunnelstation,new3536.Load);
hold on;
plot(x(p, new3536.Tunnelstation), new3536.Load, 'LineWidth', 2);
title('Load vs Chainage')
xlim([23435, 23436]);
I use subplot because I have 6 different plot that must be showed in one figure. Also I use for lop for these subplots. Does that can affect the workability of your code?
When I run these code it gives me :
lsqcurvefit stopped because the final change in the sum of squares relative to
its initial value is less than the default value of the function tolerance.
The code I gave you will just make a straight line. If you want to make something else, then you will need to have an equation. If you don't have an equation, then you can use several other techniques. For example check following packages on FEX which do a similar thing
If You have the curve-fitting toolbox, then you can also use smoothingspline option with fit(). Something like this
model = fit(new3536.Tunnelstation, new3536.Load, 'smoothingspline');
subplot(6,1,1);
scatter(new3536.Tunnelstation,new3536.Load);
hold on
plot(new3536.Tunnelstation, model(new3536.Tunnelstation), 'LineWidth', 2);
with curve fitting I gained the result. Thank you very much. I really appreciate.
I am glad to be of help.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!