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

1 visualizzazione (ultimi 30 giorni)
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

Risposta accettata

Steven Lord
Steven Lord il 20 Ott 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 Commenti
Steven Lord
Steven Lord il 20 Ott 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 il 20 Ott 2022
Modificato: Sim il 20 Ott 2022
many thanks @Steven Lord ! great !

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Line Plots in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by