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

1 回表示 (過去 30 日間)
Sim
Sim 2022 年 10 月 20 日
編集済み: Sim 2022 年 10 月 20 日
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

採用された回答

Steven Lord
Steven Lord 2022 年 10 月 20 日
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 件のコメント
Steven Lord
Steven Lord 2022 年 10 月 20 日
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 2022 年 10 月 20 日
編集済み: Sim 2022 年 10 月 20 日
many thanks @Steven Lord ! great !

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by