plot based on a value

조회 수: 2 (최근 30일)
MattC
MattC 2023년 3월 9일
댓글: MattC 2023년 3월 22일
Hi, I would like to plot a scatter plot for 3 variables in a single plot
A = 0.5; B = 0.2; C = 1.2;
scatter('X1',A,'*'); %if a<=1 then blue color else orange
scatter('X1',B,'s'); %if b<=1 then blue color else orange
scatter('X1',C,'+'); %if c<=1 then blue color else orange
yl = yline(1, '--r', 'LineWidth',1);
I want to add a legend as well which would show these values. How to achieve this?

채택된 답변

Voss
Voss 2023년 3월 9일
편집: Voss 2023년 3월 9일
% random vectors (10x1) of values between 0 and 2
AA = 2*rand(10,1);
BB = 2*rand(10,1);
CC = 2*rand(10,1);
DD = 2*rand(10,1);
EE = 2*rand(10,1);
limit = 1;
X = 1:10;
figure()
hold on
obj = [];
% use logical indexing to separate AA<=1 (blue) from AA>1 (orange)
idx = AA<=1;
obj(end+1) = scatter(X(idx),AA(idx),[],'b','*');
scatter(X(~idx),AA(~idx),[],[1 0.5 0],'*');
% use logical indexing to separate BB<=1 (blue) from BB>1 (orange)
idx = BB<=1;
obj(end+1) = scatter(X(idx),BB(idx),[],'b','s');
scatter(X(~idx),BB(~idx),[],[1 0.5 0],'s');
% use logical indexing to separate CC<=1 (blue) from CC>1 (orange)
idx = CC<=1;
obj(end+1) = scatter(X(idx),CC(idx),[],'b','+');
scatter(X(~idx),CC(~idx),[],[1 0.5 0],'+');
names = {'A','B','C'};
ylims = ylim();
ylims(1) = min(-1,ylims(1));
ylim(ylims);
idx = DD < EE;
if any(idx)
obj(end+1) = plot(X(idx),-1*ones(1,nnz(idx)),'s', ...
'MarkerFaceColor',[0.5 0.5 0.5],'MarkerEdgeColor','none');
names{end+1} = 'D<E';
end
if any(~idx)
idx2 = any([AA BB CC] > limit, 2);
if any(~idx & idx2)
obj(end+1) = plot(X(~idx & idx2),-1*ones(1,nnz(~idx & idx2)),'s', ...
'MarkerFaceColor','r','MarkerEdgeColor','none');
names{end+1} = 'A,B,C>1';
end
if any(~idx & ~idx2)
obj(end+1) = plot(X(~idx & ~idx2),-1*ones(1,nnz(~idx & ~idx2)),'s', ...
'MarkerFaceColor','g','MarkerEdgeColor','none');
names{end+1} = 'A,B,C<=1';
end
end
yl = yline(limit, '--r', 'LineWidth',1);
names{end+1} = 'limit';
legend([obj yl],names)
A = 0.5; B = 0.2; C = 1.2;
title(sprintf('Plot: A=%g, B=%g, C=%g',A,B,C))
  댓글 수: 30
Voss
Voss 2023년 3월 16일
plot(x,AA,'s',x,BB,'+','Color',Co)
plot(x,BB,'p','Color',Co)
MattC
MattC 2023년 3월 22일
Hey @Voss, I think this is not picking the right Co value from the function it picks the grey color anyways. I am not sure what's incorrect with the function. Can you please help?

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by