Creating a scatter plot with smooth lines and markers?

Hello, I am trying to create a scatter plot with smooth lines and markers in Matlab, as I can do easily in Excel. I do not think there is a way to use the scatter command to do this, so I'm asking if there is another way to plot the data in the same way (scatter format) but with smooth lines and markers? Thank you.
Ashley

4 commentaires

Can you clarify what you mean by "smooth"? Are you talking about smoothing the data, like a moving average, or just visually smooth graphics objects (like anti-aliased), or something else?
Hello, What I mean is I would like to create a scatter plot but instead of plotting just the points ( which is all I can get using the scatter command) I would like to plot the lines connecting the points as well. When I use the plot command by itself, I am not getting the desired shape of the graph that I get when I use the scatter command. I just need to create the same scatter plot but with the points connected by lines. Thank you. Ashley
I am not quite sure what you mean by a line connecting all points. Scatter diagram by definition places points on the plot.
1) If you are looking for a line connecting the points, use the plot command instead
>>plot(x,y,'-o')
2) If you want a smooth line that best fits through all your points (but not necessarily passes through them), then you need to do curve fitting . If you want a smooth line passing through all points, try using splines .
I have also tried sorting the data but I think I may have to use splines. I want a smooth line passing from 1st point to 2nd point, to 3rd point, etc. How do I use splines?

Connectez-vous pour commenter.

 Réponse acceptée

Chad Greene
Chad Greene le 18 Déc 2014
Modifié(e) : Chad Greene le 18 Déc 2014
You can plot the line with plot(x,y,'-',lineproperty,linevalue) then use hold on and plot markers of the same data with plot(x,y,'x',markerproperty,markervalue) or you can combine them into one command like this:
plot((1:5).^2,'o-','linewidth',3,'markersize',40,'markerfacecolor','g')

4 commentaires

I tried using what you recommended however the lines are being drawn from every point to every other point it seems, and I am getting one big jumbled mess. I know it works for points that do not seem to overlap as in your example, but it is not working for my data. I just want to connect the first point with the second point, to the third point, etc. This is what I used with my code:
plot(resty12013,z12013,'-')% hold on plot(resty12013,z12013,'x')
Chad Greene
Chad Greene le 18 Déc 2014
Modifié(e) : Chad Greene le 18 Déc 2014
Can you attach a picture of the Excel-produced plot that you like?
Matlab's plot command plots a line from point to point, in the order you enter the points, not necessarily in the order of lowest x to highest x. If your resty12013 data are not sorted, the lines between each point will look like spaghetti. As Star Strider suggested, try sorting your data by
B = sortrows([resty12013(:) z12013(:)]);
resty12013sorted = B(:,1);
z12013sorted = B(:,2);
Then plot the sorted values.
The data are already sorted according to the independent variable. I've attached a picture of what the graph's shape looks like when it is done using scatter command. This is how I would like it to stay however with lines connecting each point consecutively. (1st point connected to 2nd point, connected to 3rd point, etc.)
I suspect something in your data set is out of order.
indep = 1:.1:10;
dep = sin(indep)+rand(size(indep));
plot(dep,indep,'ro-')
looks good. Then get the data out of order:
rp = randperm(91);
plot(dep(rp),indep(rp),'ro-')

Connectez-vous pour commenter.

Plus de réponses (2)

You can use both the scatter and line commands like this...
x = [1 2 3]
y = [1 3 9]
scatter(x,y)
hold on
line(x,y)
xlim([0 4])
ylim([0 10])

2 commentaires

I also tried using what you recommended however the lines are being drawn from every point to every other point it seems, and I am getting one big jumbled mess. I know it works for points that do not seem to overlap as in your example, but it is not working for my data. I just want to connect the first point with the second point, to the third point, etc. This is what I used with my code:
x = resty12013 %a column of data points
y = z12013 % another column of data points
scatter(x,y)
hold on
line(x,y)
xlim([0 10000])
ylim([419 430])
Sounds like maybe you are not sorting correctly. Anyway, here are three examples in case it helps:
%%plot 1
figure
x = [1 2 3 1.5];
y = [1 4 9 5];
scatter(x,y)
hold on
line(x,y)
xlim([0 4])
ylim([0 10])
%%plot 2
figure
sorted=sortrows([x' y']);
sorted_x = sorted(:,1);
sorted_y = sorted(:,2);
scatter(sorted_x,sorted_y)
hold on
line(sorted_x,sorted_y)
xlim([0 4])
ylim([0 10])
%%plot 3
figure
fitx=linspace(0,4,100);
fity = interp1(sorted_x,sorted_y,fitx,'spline');
scatter(sorted_x,sorted_y)
hold on
line(fitx,fity)
xlim([0 4])
ylim([0 10])

Connectez-vous pour commenter.

Catégories

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by