compact way to plot "duration" elements, with different colors

1 visualización (últimos 30 días)
Sim
Sim el 20 de Oct. de 2022
Editada: Sim el 20 de Oct. de 2022
Do you know a compact way to plot a set of duration elements in different colors, without the loop for ?
% input (set of durations)
a = duration({ ...
'00:01:10'
'00:01:35'
'00:01:09'
'00:04:40'
'01:32:36'
'00:01:16'
'00:01:38'
'00:13:52'
'00:01:28'
'00:01:02'
'00:01:35'
'00:01:11'
'00:04:21'})
a = 13×1 duration array
00:01:10 00:01:35 00:01:09 00:04:40 01:32:36 00:01:16 00:01:38 00:13:52 00:01:28 00:01:02 00:01:35 00:01:11 00:04:21
% colors for the input elements (duration)
color = jet(3);
index_color = [1 1 1 1 2 1 1 3 1 1 1 1 1];
% plot both line and points (durations): this is the part I would like to
% re-write in a more compact way!
hold on
plot(a,'-','color','black')
for i = 1 : length(a)
plot(i,a(i),'o','markerfacecolor',color(index_color(i),:))
end
hold off

Respuesta aceptada

Steven Lord
Steven Lord el 20 de Oct. de 2022
a = duration({ ...
'00:01:10'
'00:01:35'
'00:01:09'
'00:04:40'
'01:32:36'
'00:01:16'
'00:01:38'
'00:13:52'
'00:01:28'
'00:01:02'
'00:01:35'
'00:01:11'
'00:04:21'});
index_color = [1 1 1 1 2 1 1 3 1 1 1 1 1];
scatter(1:numel(a), a, [], index_color, 'filled')
  3 comentarios
Steven Lord
Steven Lord el 20 de Oct. de 2022
All the markers created by one call to plot with one set of data inputs must have the same color. You could call plot with multiple sets of data inputs to create multiple lines at once with different colors as long as those colors are from the standard list available for use in a linespec. In the example below I use red circles, black plus symbols, and cyan triangles.
a = duration({ ...
'00:01:10'
'00:01:35'
'00:01:09'
'00:04:40'
'01:32:36'
'00:01:16'
'00:01:38'
'00:13:52'
'00:01:28'
'00:01:02'
'00:01:35'
'00:01:11'
'00:04:21'});
n = numel(a);
plot(1:3:n, a(1:3:n), 'ro', 2:3:n, a(2:3:n), 'k+', 3:3:n, a(3:3:n), 'c^')
Sim
Sim el 20 de Oct. de 2022
Editada: Sim el 20 de Oct. de 2022
many thanks @Steven Lord ! great !

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Line Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by