Specify Square Marker in a Plot

조회 수: 177 (최근 30일)
MarshallSc
MarshallSc 2022년 6월 19일
댓글: Jeffrey Clark 2022년 6월 19일
For a plot like below:
X = (1:25);
Y = [2 1 2 2 2 3 1 4 3 2 1 2 3 7 11 8 5 2 3 4 5 8 5 5 7];
fig1 = figure();
set(fig1,'color','white')
plot(X,Y,'-s',...
'LineWidth',2,...
'MarkerSize',10,...
'MarkerEdgeColor','r',...
'MarkerFaceColor',[0.3,0.3,0.3])
How can I specify a red square marker only for the points with Y value equal or bigger than 8, and for the rest a blue square? So for the figure above, I want the square markers at points with X value of 15, 16 and 22 to be red and the rest to be blue. Thank you!
  댓글 수: 1
Jeffrey Clark
Jeffrey Clark 2022년 6월 19일
@MarshallSc, plot the line ('-') separate from the points, then with hold on do two plots of only the marker ('s'). one where X(Y>8) and Y(Y>8) and the other X(Y<=8) and Y(Y<=8). Using something like selected = Y>8 you can then use selected and ~selected in the X and Y plot selections.

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

채택된 답변

Voss
Voss 2022년 6월 19일
You can break it up into multiple lines:
X = (1:25);
Y = [2 1 2 2 2 3 1 4 3 2 1 2 3 7 11 8 5 2 3 4 5 8 5 5 7];
fig1 = figure();
set(fig1,'color','white')
plot(X,Y,'-',...
'LineWidth',2);
hold on
idx = Y >= 8;
plot(X(idx),Y(idx), ...
'Marker','s', ...
'LineStyle','none', ...
'MarkerSize',10,...
'MarkerEdgeColor','r',...
'MarkerFaceColor','r');%[0.3,0.3,0.3])
plot(X(~idx),Y(~idx), ...
'Marker','s', ...
'LineStyle','none', ...
'MarkerSize',10,...
'MarkerEdgeColor','b',...
'MarkerFaceColor','b');%[0.3,0.3,0.3])

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by