I'm very new to Matlab. I'm trying to plot X
, where X
is an 100x1 vector, against Y
, which is an 100x10 matrix. I want the result to be X
vs 10 different Y
values all in the same graph, different colors for each column. The only way I can think of plotting each column of this matrix is by using the hold
command, but then I have to split it up so I get each column individually. Is there an easy way to do this?
Asked
Active
Viewed 898 times
1
2 Answers
1
Use repmat
to expand X
to be the same size as Y
. Try plotting them with plot(X,Y)
and if it looks strange, transpose each one (plot(X',Y')
).
You can use linespec arguments to select linestyle, marker style, etc. For example, plot(X,Y,'.')
would indicate a point at each vertex with no connecting lines.

tmpearce
- 12,523
- 4
- 42
- 60
-
Ah this does work! Thank you! Why do you have to do it this way with `repmat` when it seems like there should be a more intuitive way to do this - it doesn't seem right for scatter plots to only take vectors for y. – rb612 Feb 10 '17 at 04:26
-
It works because when you have two matrices, it plots each column versus the other. If you just use `plot(X,Y,'.')` as-is without repmat, what happens? – tmpearce Feb 10 '17 at 13:40
1
You don't need to use repmat
, just use plot
instead of scatter
:
plot(X,Y,'o')
Here's an example:
% some arbitrary data:
X = linspace(-2*pi,2*pi,100).'; % size(X) = 100 1
Y = bsxfun(@plus,sin(X),rand(100,10)); % size(Y) = 100 10
% you only need the next line:
plot(X,Y,'o')
legend('show')

EBH
- 10,350
- 3
- 34
- 59