Clear Filters
Clear Filters

StackedPlot with separate events

36 views (last 30 days)
Alex Newell
Alex Newell on 2 Jul 2024 at 16:08
Commented: Alex Newell on 2 Jul 2024 at 22:25
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 Comments
Noah Prisament
Noah Prisament on 2 Jul 2024 at 19:26
Moved: Voss on 2 Jul 2024 at 19:28
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.
Alex Newell
Alex Newell on 2 Jul 2024 at 22:25
Awesome, thank you!

Sign in to comment.

Answers (1)

Voss
Voss on 2 Jul 2024 at 16:50
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 Comments
Voss
Voss on 2 Jul 2024 at 19:36
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.
Alex Newell
Alex Newell on 2 Jul 2024 at 22:25
Ok, thanks a lot!

Sign in to comment.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!