Creating a scatter plot with smooth lines and markers?
232 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Ashley
le 16 Déc 2014
Réponse apportée : Biswas Lohani V K
le 15 Mai 2016
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
Chetan Rawal
le 18 Déc 2014
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 .
Réponse acceptée
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')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/177204/image.png)
4 commentaires
Chad Greene
le 19 Déc 2014
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-')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/180656/image.png)
looks good. Then get the data out of order:
rp = randperm(91);
plot(dep(rp),indep(rp),'ro-')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/180657/image.png)
Plus de réponses (2)
Scott Webster
le 18 Déc 2014
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])
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/177206/image.png)
2 commentaires
Scott Webster
le 19 Déc 2014
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])
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/180653/image.png)
%%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])
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/180654/image.png)
%%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])
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/180655/image.png)
Voir également
Catégories
En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!