# Legend Problem in Scatter Plotting

39 views (last 30 days)
Ritesh Chandra Tewari on 27 Jan 2021
Commented: Star Strider on 27 Jan 2021
[m,n] = size(y);
x1 = X(:,1);
x2 = X(:,2);
for j=1:1:n
for i=1:1:m
if y(i,j)==1
scatter(x1(i,j), x2(i,j), 20, y(i,j),'k', '+');
else
scatter(x1(i,j), x2(i,j), 10, y(i,j),'y', 'filled');
hold on;
end
end
end
X is a 300x2 matrix consisting random data and y is a 300x1 matrix consisting only 0 and 1. I want to plot a 2D scatter graph of x1 and x2. Based on the value of y i.e. 0 or 1 the marker of the plot will be changed. But both the legend showing the same marker. I dont want to use any other plotting function , rather want to stick to scatter. The plot is showing like:

Star Strider on 27 Jan 2021
See if this does what you want:
[m,n] = size(y);
x1 = X(:,1);
x2 = X(:,2);
for j=1:1:n
for i=1:1:m
if y(i,j)==1
hs1 = scatter(x1(i,j), x2(i,j), 20, y(i,j),'k', '+');
else
hs2 = scatter(x1(i,j), x2(i,j), 10, y(i,j),'y', 'filled');
hold on;
end
end
end
I obviously cannot test this with your data, so I am posting it as UNTESTED CODE.
.
Ritesh Chandra Tewari on 27 Jan 2021
Thanks buddy!
Star Strider on 27 Jan 2021
As always, my pleasure!

### Categories

Find more on Discrete Data Plots in Help Center and File Exchange

R2018a

### Community Treasure Hunt

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

Start Hunting!