Specify Square Marker in a Plot

271 ビュー (過去 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 Center および File ExchangeGraphics についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by