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

2 次查看(过去 30 天)
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^')

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by