How do I join individual scatter points with a line?
27 views (last 30 days)
Show older comments
I'm trying to plot a set of 10 data points, and connect corresponding pairs of them together, each with different colour. I'm very unexperienced, so be prepared for horrendously inefficient code:
x1 = 0.967;
x1b = 0.960;
y1 = 0.14;
y1b = 0.172;
x2 = 0.952;
x2b = 0.942;
y2 = 0.272;
y2b = 0.331;
x3 = 0.937;
x3b = 0.925;
y3 = 0.397;
y3b = 0.479;
x4 = 0.923;
x4b = 0.909;
y4 = 0.516;
y4b = 0.619;
x5 = 0.910;
x5b = 0.894;
y5 = 0.630;
y5b = 0.751;
sz1 = 40;
sz2 = 30;
c1 = 'k';
c2 = 'b';
c3 = 'g';
c4 = 'r';
c5 = 'c';
sym = 'o';
xlim([0.89 1.01])
ylim([0 0.8])
scatter(x1,y1,sz1,c1,sym,'filled'); hold on;
scatter(x1b,y1b,sz2,c1,sym,'filled'); hold on;
scatter(x2,y2,sz1,c2,sym,'filled'); hold on;
scatter(x2b,y2b,sz2,c2,sym,'filled'); hold on;
scatter(x3,y3,sz1,c3,sym,'filled'); hold on;
scatter(x3b,y3b,sz2,c3,sym,'filled'); hold on;
scatter(x4,y4,sz1,c4,sym,'filled'); hold on;
scatter(x4b,y4b,sz2,c4,sym,'filled'); hold on;
scatter(x5,y5,sz1,c5,sym,'filled'); hold on;
scatter(x5b,y5b,sz2,c5,sym,'filled'); hold on;
line(x1,x2); hold on;
I have no doubt there is a much more efficient way to do this, but I couldn't figure it out, so I went for the brute force method. All there's left to do is connect the pairs together. I'd like to connect a line between the points (x1,y1) and (x1b, y1b), another line between the points (x2,y2) and (x2b,y2b), and so on. How do I do this?
2 Comments
Stephen23
on 26 Apr 2017
Edited: Stephen23
on 26 Apr 2017
1) The main thing you really really need to do is to use vectors/matrices/arrays to store data. The name MATLAB comes from "MATrix LABoratory", not from "lets split up the data into lots of separate variables". Once you start to use vectors/matrices/arrays to store your data, then your code will be much simpler, more efficient, and easier to work with.
2) Creating numbered variables is also a bad sign: these are basically fake indices, which should be turned into real indices (see point one). Some beginners want to try and access their numbered variables, but that is a path that you really do NOT want to go down:
3) You might find this useful to read:
4) Always read the documentation (see point three). So you want to plot lines.... then read the plot help. It even has working examples for you to try. Did you try them?
5) The best place to start learning how to use MATLAB effectively are the introductory tutorials, which are highly recommended for all beginners:
Answers (1)
Star Strider
on 26 Apr 2017
You need to put the x and y variables in matrices. This will make plotting them significantly easier. I did that here using the whos function and the (occasionally useful if often vilified) eval function to create them since I didn’t want to type all of them into matrices myself. (I’m lazy.)
See if this does what you want:
vars = whos('x*','y*'); % First, Create Matrices From The Individual Variables
vns = cellfun(@(x)x, {vars.name}, 'Uni',0);
for k1 = 1:2:length(vns)
idx = k1/2 + 0.5;
xy(idx,:) = [eval(vns{k1}) eval(vns{k1+1})];
end
xm = xy(1:5,:); % ‘x’ Matrix
ym = xy(6:10,:); % ‘y’ Matrix
figure(1)
plot(xm(1,:), ym(1,:))
hold on
for k1 = 2:size(xm,1)
plot(xm(k1,:), ym(k1,:))
end
hold off
grid
Format the plots and markers as you want them. This routine just does the basic plotting.
1 Comment
Stephen23
on 26 Apr 2017
Edited: Stephen23
on 26 Apr 2017
@David Anthony: note that Star Strider's code does something not recommended by the MATLAB documentation in order to place all of the data into two arrays xm and ym, i.e. to fix the fact that the data was defined in lots of separate variables. In reality you should simply define xm and ym to begin with.
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!