MATLAB Answers

# Can you have lines between circles on a scatter plot?

2 views (last 30 days)
Josh Coyston on 4 May 2021
Commented: Star Strider on 4 May 2021
I am plotting two sets of data onto one plot, and I would like to have lines drawn connecting the different plots when they have the same value.
For a overly simple example:
a = [50:199];
b = [75:224];
i = [1:150];
scatter( i, [a;b] ) I would like a straight line joining dots together than have value 75, a line joining dots that have value 76, etc etc. Ideally the lines would be a different colour to the dots, but they themselves can be the same colour.
(I know in this example it would create a rather unhelpful-looking plot, but as said, this is much simpler than the types of scatters I'm looking at).
I'm quite new to Matlab, so I'm not sure whether this is something that's ridiculously easy or rather complex!
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Star Strider on 4 May 2021
Try this —
a = [50:199];
b = [75:224];
i = [1:150];
scatter( i, [a;b] )
[ia,ib] = (ismember(a,b));
aidx = find(ia);
hold on
plot([i(aidx); i(ib(aidx))], ([1; 1]*a(aidx)), '-g', 'LineWidth',0.5)
hold off The ismember call finds the elements common to both vectors. The rest is just addressing the correct elements of both of them, and plotting the horizontal lines.
.
##### 2 CommentsShowHide 1 older comment
Star Strider on 4 May 2021
As always, my pleasure!
It was something of a challenge for me as well. The ismember call was straightforward, however getting the indexing correct and then plotting the matrices correctly required some experimentation.

Sign in to comment.

### More Answers (1)

VBBV on 4 May 2021
Edited: VBBV on 4 May 2021
%if true
a = [50:199];
b = [75:224];
i = [1:150];
scatter( i, [a;b] );hold on;
for k = 1:length(a)
line([i(k) i(k)],[a(k) b(k)],'Color','black','LineStyle','-');
end
Do you mean some thing like this?
##### 2 CommentsShowHide 1 older comment
Cris LaPierre on 4 May 2021
MATLAB will treat columns as a distinct series. You can take advantage of that.
a = [50:199];
b = [75:224];
i = [1:150];
scatter(i,[a;b]);
hold on;
plot([i;i],[a;b],'k')
hold off Sign in to comment.

R2021a

### Community Treasure Hunt

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

Start Hunting!