Specify Square Marker in a Plot

53 views (last 30 days)
MarshallSc
MarshallSc on 19 Jun 2022
Commented: Jeffrey Clark on 19 Jun 2022
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 Comment
Jeffrey Clark
Jeffrey Clark on 19 Jun 2022
@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.

Sign in to comment.

Accepted Answer

Voss
Voss on 19 Jun 2022
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])

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by