StackedPlot with separate events
5 次查看(过去 30 天)
显示 更早的评论
I'd like to make a stacked plot with each plot showing different events (each plot's own events only). How could I fix this code to show only the proper data's corresponding events? (apologies for my clumsy coding)
%make timetable
times=[1, 2, 5]';
s=seconds(times);
variable=[3, 4, 5]';
T=table(s,variable);
TT=table2timetable(T);
%makes event table
EventTime = seconds(4);
lengths = seconds(1);
Events = eventtable(EventTime,EventLengths=lengths);
%Assign event table to time table
TT.Properties.Events=Events;
%make timetable
variable2=[6, 8, 9]';
T2=table(s,variable2);
TT2=table2timetable(T2);
%makes event table
EventTime2 = seconds(1);
lengths2 = seconds(1);
Events2 = eventtable(EventTime2,EventLengths=lengths2);
%Assign event table2 to time table2
TT2.Properties.Events=Events2;
stackedplot(TT,TT2)
2 个评论
Noah Prisament
2024-7-2
移动:Voss
2024-7-2
Hi Alex, I have submitted an Enhancement Request on your behalf to the relevant development team to support this functionality in a future update or release of MATLAB.
回答(1 个)
Voss
2024-7-2
Maybe something like this is good enough:
%make timetable
times=[1, 2, 5]';
s=seconds(times);
variable=[3, 4, 5]';
T=table(s,variable);
TT=table2timetable(T);
%makes event table
EventTime = seconds(4);
lengths = seconds(1);
Events = eventtable(EventTime,EventLengths=lengths);
%Assign event table to time table
TT.Properties.Events=Events;
%make timetable
variable2=[6, 8, 9]';
T2=table(s,variable2);
TT2=table2timetable(T2);
%makes event table
EventTime2 = seconds(1);
lengths2 = seconds(1);
Events2 = eventtable(EventTime2,EventLengths=lengths2);
%Assign event table2 to time table2
TT2.Properties.Events=Events2;
f = figure();
tl = tiledlayout(f,2,1);
ax = gobjects(1,2);
h = gobjects(1,2);
col = colororder(f);
ax(1) = nexttile(tl);
h(1) = plot(TT.s,TT.variable,'Color',col(1,:));
xregion(ax(1),Events.Time,Events.Time+Events.EventLengths);
ylabel(ax(1),'variable','Rotation',0);
ax(1).Box = 'off';
ax(1).XAxis.Visible = 'off';
ax(2) = nexttile(tl);
h(2) = plot(TT2.s,TT2.variable2,'Color',col(2,:));
xregion(ax(2),Events2.Time,Events2.Time+Events2.EventLengths);
ylabel(ax(2),'variable2','Rotation',0);
xlabel(ax(2),'s');
ax(2).Box = 'off';
legend(h,{'TT','TT2'},'Location','NorthOutside','Orientation','horizontal')
linkaxes(ax,'x')
3 个评论
Voss
2024-7-2
Unfortunately, MathWorks doesn't allow us much customization of stackedplots at the present time. Maybe someone else can share a workaround using a stackedplot.
Note that in my answer, all the axes are linked in the 'x' dimension (linkaxes(ax,'x')), so when you zoom/pan on one axes, the others will update automatically. It may not be as useful as the "linked datatip" you'd get in a stackedplot, but it may be of some use.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!