Legend Problem in Scatter Plotting

조회 수: 10 (최근 30일)
Ritesh Chandra Tewari
Ritesh Chandra Tewari 2021년 1월 27일
댓글: Star Strider 2021년 1월 27일
[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.
.
  댓글 수: 2
Ritesh Chandra Tewari
Ritesh Chandra Tewari 2021년 1월 27일
Thanks buddy!
Star Strider
Star Strider 2021년 1월 27일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Discrete Data Plots에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by