Plot connected vertical points with different marks

I have a vector x=[ 1 2 3 4], and a matrix of size: 3x4.
I want to plot something like this:
Update: the number of vertical points can be different (in my example I used 3, but they can be from 2 to 5).
Update 2: "x" mark is not the center, it might happen than "x" mark is lower or higher than the other two points, for a particular x value.
Any ideas?

 Respuesta aceptada

Try something like this —
x=[ 1 2 3 4];
M = rand(3,4);
Mx = max(M);
Mn = min(M);
figure
plot(x, Mx, 'd')
hold on
plot(x, Mn, 'o')
plot([x; x], [Mx; Mn], '--k')
hold off
grid
xlim([0 5])
.I am not certain what the ‘x’ markers in the centres of the lines represent, so I did not include them. However they could llikely be added with one plot call.
.

4 comentarios

Rub Ron
Rub Ron el 20 de Ag. de 2021
Editada: Rub Ron el 20 de Ag. de 2021
hi thanks, actually there can be more points than 3 points, So the "x" point is not the center, but an independent point, than can be lower or higher than the other points.
x = 1:4;
m = [0.4 0.3 0.5 0.4; 0.25 0.25 0.30 0.20; 0.15 0.20 0.11 0.11];
scatter(x,m(1,:),'d')
hold on
scatter(x,m(2,:),'x')
scatter(x,m(3,:),'o')
for i = 1:4
plot([x(i) x(i)],[m(1,i) m(3,i)],'r')
end
xlim([0 5])
ylim([0 0.6])
xlabel('x')
ylabel('y')
@Rub Ron My code can likely be adapted reasonably easily to any size matrix and compatible-size independent variable vector. It would help to have an actual matrix and to know what you want to display. The ‘x’ markers are then just another plot or scatter call within the hold block.
@Kevin Holly Thank you!
Adding the ‘x’ markers to my previous code —
x=[ 1 2 3 4];
M = rand(3,4);
Mx = max(M);
Mn = min(M);
xv = rand(size(x));
figure
plot(x, Mx, 'd')
hold on
plot(x, Mn, 'o')
plot([x; x], [Mx; Mn], '--k')
plot(x, xv, 'xm', 'MarkerSize',10) % Added
hold off
grid
axis([0 5 0 1]) % Optional
.
Rub Ron
Rub Ron el 20 de Ag. de 2021
@Kevin Holly thanks!

Iniciar sesión para comentar.

Más respuestas (1)

Try this:
markerSize = 14;
lineWidth = 2;
x = [1 2 3 4]
y = 0.1 + 0.4 * rand(3, 4)
[rows, columns] = size(y)
for col = 1 : columns
yTop = max(y(:, col));
yBottom = min(y(:, col));
yMid = setdiff(y(:, col), [yTop; yBottom]);
% Plot line
plot([x(col), x(col)], [yBottom, yTop], 'b-', 'LineWidth', 3);
hold on;
% Plot markers
plot(x(col), yTop, 'bd', 'LineWidth', lineWidth, 'MarkerSize', markerSize);
plot(x(col), yBottom, 'bo', 'LineWidth', lineWidth, 'MarkerSize', markerSize);
plot(x(col), yMid, 'bx', 'LineWidth', lineWidth, 'MarkerSize', markerSize);
end
grid on;

2 comentarios

Rub Ron
Rub Ron el 20 de Ag. de 2021
Hi, thank you. The number of vertical points can be different, from 2 to 5 (in my example I used 3). Can it be generalized? Sorry if I was not explicit.
Yeah, that was not specified. But it looks like you've accepted an answer so I guess you got it all figured out for the case of variable number of rows.

Iniciar sesión para comentar.

Categorías

Más información sobre Labels and Annotations en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 20 de Ag. de 2021

Comentada:

el 20 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by