Borrar filtros
Borrar filtros

Specify Square Marker in a Plot

280 visualizaciones (últimos 30 días)
MarshallSc
MarshallSc el 19 de Jun. de 2022
Comentada: Jeffrey Clark el 19 de Jun. de 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 comentario
Jeffrey Clark
Jeffrey Clark el 19 de Jun. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Voss
Voss el 19 de Jun. de 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])

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by