Timetable Variable grouped by year and stackedplot

I have the following unstacked timetable that I would like to plot with the stackedplot function.
The stackedplot should have the Day/Month as the X-Axis and it should be "stacked" or grouped by the year.
Should the grouping be done by some timetable function or should I just loop through the timetable with the required conditions? Thanks!
Datum DailyAvgTemp
__________ ____________
01.01.1956 -6.8
02.01.1956 -3.7
03.01.1956 -4.5
04.01.1956 -5.2
05.01.1956 0
06.01.1956 -3.8
...
05.11.2018 -2
06.11.2018 -2.8

 Accepted Answer

Steps taken to break apart a timetable by years and plot in stackedplot
  1. The timetable is broken apart by year using arrayfun and each subtable is stored in a cell array TTbyYear.
  2. The dates of each subtable are shifted to 1952, preserving the month and day. Note that 1952 is a leapyear. It's important that all dates share the same year and since some years in your data may be leapyears, the shared year should be a leapyear.
  3. The subtables are joined back together using outerjoin where each year has its own column and the variable names in the new table TTy are the original years.
  4. The new table TTy is sent to stackedplot().
Demo
% Create demo timetable
TT = timetable(round(rand(3652,1)*100)/10-5,...
'StartTime',datetime(1956,1,1,'Format','dd.MM.yyyy'),...
'TimeStep',days(1),...
'VariableNames',{'DailyAvgTemp'});
TT.Properties.DimensionNames{1} = 'Datum';
head(TT) % show sample
ans = 8x1 timetable
Datum DailyAvgTemp __________ ____________ 01.01.1956 -1.1 02.01.1956 -3 03.01.1956 2.4 04.01.1956 2.4 05.01.1956 -4.2 06.01.1956 -1.8 07.01.1956 -4 08.01.1956 -4.4
% Break apart timetable by year
TTyears = year(TT.Datum);
unqYears = unique(TTyears);
TTbyYear = arrayfun(@(y){TT(TTyears==y,:)},unqYears);
% Join the tables back into a timetable with 1 column for each year
baseYear = 1956; % should be a leap year
TTy = timetable();
for i = 1:numel(TTbyYear)
% Change year to base year
TTbyYear{i}.Datum = datetime(baseYear,month(TTbyYear{i}.Datum),day(TTbyYear{i}.Datum));
% join tables
TTy = outerjoin(TTy, TTbyYear{i});
end
% Change variable names to years
TTy.Properties.VariableNames = compose('%d',unqYears);
% Change format of datetime values to hide the year
TTy.Datum.Format = 'dd.MM';
head(TTy) % Show sample
ans = 8x10 timetable
Datum 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 _____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ 01.01 -1.1 -0.6 2.4 2.3 -2.7 -1.7 -3 4.8 2.2 -2 02.01 -3 -3.1 -0.1 4.4 -0.4 0.1 -3.3 -2.3 2.2 3.9 03.01 2.4 4.6 0.3 -2.1 0.8 -2.3 2.9 1.8 -2.5 -3.5 04.01 2.4 -4.6 -0.1 1 -3.4 1.1 -2.8 1.9 3 4 05.01 -4.2 -2.6 -2.5 -1.4 4.4 2.1 3.6 -2.2 -2.1 4.5 06.01 -1.8 -4.4 -3.7 4.9 0 -2.9 4.5 -0.9 -0.6 4.4 07.01 -4 3.8 -3.4 -4.6 1 -0.2 2.8 2.6 2.5 -1 08.01 -4.4 -0.6 2.1 -0.4 1.8 0.6 -0.4 1.3 -3.1 -3.9
% Plot
stackedplot(TTy)

2 Comments

Thank you Sir!
This answered all my questions!
Glad I could help.
I just made a small change to the demo. The variable baseYear was not used and I hard-coded the year within the loop instead. That was fixed so that baseYear is used within the loop.

Sign in to comment.

More Answers (1)

For stackedplot to work as you intend, you would have to make each year's data its own variable (column). There is no timetable function for that. Also note that stackedplot has a maximum of 25 variables (1 variable = 1 plot). It looks like you might have 63.
I do think turning your table into a timetable, or at the least making your first column datetimes will make it much easier to work with the time data.

4 Comments

Thank you for your reply!
The dataset is already a timetable.
Would you suggest using some other plotting method or would you really create new variable as "years" ?
You don't need it, and year(TT.date) can extract it easily enough.
While not ideal if there are 63 years of data, one way to show this in a single plot would be this.
% create a sample dataset
datum = (datetime(1956,1,1):calmonths(1):datetime(1960,12,1))';
avgTemp = randi(100,[length(datum),1]);
TT = timetable(datum,avgTemp)
TT = 60x1 timetable
datum avgTemp ___________ _______ 01-Jan-1956 28 01-Feb-1956 49 01-Mar-1956 27 01-Apr-1956 64 01-May-1956 12 01-Jun-1956 55 01-Jul-1956 34 01-Aug-1956 51 01-Sep-1956 12 01-Oct-1956 20 01-Nov-1956 89 01-Dec-1956 69 01-Jan-1957 9 01-Feb-1957 14 01-Mar-1957 7 01-Apr-1957 63
% Plot grouping by year
h=gscatter(month(TT.datum),TT.avgTemp,year(TT.datum));
set(h,'LineStyle','-');
xticks(1:12);
xticklabels(["Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"]);
legend('Location','eastoutside')
If you were set on using a stacked plot, you could break apart your data into 3-5 year segments and show multiple years in each axes.
Looks like I incorrectly assumed dates were mm-dd-yyyy, which is simpler to solve. If it's dd-mm-yyyy, it gets a little more challenging.

Sign in to comment.

Categories

Products

Release

R2020b

Asked:

on 14 Dec 2020

Commented:

on 14 Dec 2020

Community Treasure Hunt

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

Start Hunting!