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 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