Timetable Variable grouped by year and stackedplot

조회 수: 10 (최근 30일)
John Barron
John Barron 2020년 12월 14일
댓글: Adam Danz 2020년 12월 14일
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

채택된 답변

Adam Danz
Adam Danz 2020년 12월 14일
편집: Adam Danz 2020년 12월 14일
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
John Barron
John Barron 2020년 12월 14일
Thank you Sir!
This answered all my questions!
Adam Danz
Adam Danz 2020년 12월 14일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Cris LaPierre
Cris LaPierre 2020년 12월 14일
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
Adam Danz
Adam Danz 2020년 12월 14일
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.
Cris LaPierre
Cris LaPierre 2020년 12월 14일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Discrete Data Plots에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by