How to use stackedplot such that each graph has multiple lines AND x-axes is unevenly spaced continuous variable?

5 views (last 30 days)
I am attempting to create a stacked plot with three stacked plots/windows. I wish to include two lines on the first plot. In addition, I would like to be able to set the x-axis to be a specific vector of times that are not evenly spaced

Answers (1)

Adam Danz
Adam Danz on 14 Dec 2021
Edited: Adam Danz on 16 Dec 2021
> I wish to include two lines on the first plot.
It's easiest to work with tables or timetables when using stackedplot. If you're not already working with a table|timetable variable, convert your data to a table or timetable. Setting the VariableNames property of your table will provide axis labels to stackedplot.
% Create timetable
TT = array2timetable(rand(10,4)+[0 0 10 100],...
'RowTimes', datetime(2020,01,01,00,00,00) + minutes((0:9)'), ...
'VariableNames', {'A','B','C','D'})
TT = 10×4 timetable
Time A B C D ____________________ ________ ________ ______ ______ 01-Jan-2020 00:00:00 0.52566 0.055342 10.615 100.18 01-Jan-2020 00:01:00 0.095467 0.8096 10.942 100.94 01-Jan-2020 00:02:00 0.68824 0.035538 10.667 100.33 01-Jan-2020 00:03:00 0.077908 0.9684 10.289 100.69 01-Jan-2020 00:04:00 0.43765 0.34152 10.324 100.45 01-Jan-2020 00:05:00 0.56174 0.21214 10.473 100.31 01-Jan-2020 00:06:00 0.13971 0.39024 10.166 100.29 01-Jan-2020 00:07:00 0.74677 0.059066 10.785 100.55 01-Jan-2020 00:08:00 0.63332 0.30464 10.279 100.31 01-Jan-2020 00:09:00 0.96443 0.67984 10.48 100.4
% Specify column numbers for each axis
sph = stackedplot(TT,{[1,2],3,4});
> I would like to be able to set the x-axis to be a specific vector of times that are not evenly spaced
I assume you mean the x-axis-ticks. If you only want to select a specific set of rows of the table, you can achieve that by indexing the rows of the table. Example: stackedplot(TT(idx,:),___)
% To specify x-ticks
ax = findobj(sph.NodeChildren, 'Type','Axes'); % get axis handles
set(ax, 'XTick', TT.Time([1,4,6,9,10])) % set xtick
grid on % to show that xticks were applied to all axes
If you want to change the datetime format of the tick labels, use xtickformat(ax(1),'HH:mm:ss') or ax(1).XAxis.TickLabelFormat='HH:mm:ss';

Categories

Find more on Discrete Data Plots in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!