Legend Problem in Scatter Plotting

8 次查看(过去 30 天)
[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
legend("Admitted","Not Admitted");
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
Star Strider 2021-1-27
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
legend([hs1(1),hs2(1)],"Admitted","Not Admitted");
I obviously cannot test this with your data, so I am posting it as UNTESTED CODE.
.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by