Reputation: 1315
I'm plotting two vectors against one another. I would like to vary the symbol used to plot each point based on the corresponding value in a third vector.
In other words, if I'm plotting X and Y, I know I can make each plot point display as '*' like so:
plot (X, Y, "*")
But how can I involve a third vector Z such that '*' is displayed for some values of Z, and '+' is displayed for others?
Upvotes: 2
Views: 3866
Reputation: 527
Can also be done by getting the indices of elements of Z for which +/* are to printed in 2 separate arrays using find
method.
Considering that you want to print (+) for positive values of Z and (*) for negative values, the following code will do it:
pos = find(Z > 0); neg = find(Z <= 0);
plot(X(pos), Y(pos), 'k+', X(neg), Y(neg), 'k*');
Upvotes: 4
Reputation: 1413
code below will print 'ro' for z > 0 and 'bx' for z < 0
plot(x(z>0), y(z>0), 'ro', x(z<0), y(z<0), 'bx')
Upvotes: 2
Reputation: 15930
Try something like this:
x = [1 2 3];
y = [1 4 9];
z = {'*' '+' '*'};
for i_=1:length(x)
eval(["plot(x(" num2str(i_) "),y(" num2str(i_) "),'" z{i_} "')"])
hold on
end
This essentially makes n
plots, where n
is the length of x
and y
. If you want the point color to change for each point you can use hold all
instead of hold on
. If you want the point style to be conditional on the value of y
, you can do
x = [1 2 3];
y = [1 4 9];
z = {'*' '+' '*'};
for i_=1:length(x)
if (y(i_) > 1)
z{i_} = '*';
else
z{i_} = '+';
end
eval(["plot(x(" num2str(i_) "),y(" num2str(i_) "),'" z{i_} "')"])
hold on
end
Upvotes: 2