How to plot monthly data for 31 years on the same plot?

9 ビュー (過去 30 日間)
Leo Tu
Leo Tu 2021 年 6 月 30 日
コメント済み: Lejla Latifovic 2022 年 4 月 29 日
I have a table with the first column labelled month_Date. It contains the months from Jan 1987 to Jan 2020. The Second column is just a groupcount. The third column is labelled mean_MeanSoilTemperatureAt1MdegCIDWMean and it contains the mean soil temperature for each of the months. I need to make a plot such that the x-axis is labelled with the months and the y-axis labelled soil temperature. Then, I need the lines of the plot to be each years values and for each line to be a specific colour. If anyone could help, it would be much appreciated.
  1 件のコメント
Leo Tu
Leo Tu 2021 年 6 月 30 日
Update: I orginally had a table called T with dates from 01/01/1987 00:00 to 01/01/2020 00:00 and I used the following script to group the dates into months and to find the mean of soil temperature for each month:
TT = groupsummary(T,1,"Month","Mean",38);
where 1 is the column number for the dates in T and 38 is the column number for soil temperature in T.
Then TT is my new table as mentioned in my original question post.

サインインしてコメントする。

採用された回答

Seth Furman
Seth Furman 2021 年 7 月 1 日
編集済み: Seth Furman 2021 年 7 月 1 日
It is probably easier to store the data in a timetable and use retime to get the monthly averages.
tt = timetable(datetime(2000,1,1)+calmonths(1:100)',(1:100)')
tt = 100×1 timetable
Time Var1 ___________ ____ 01-Feb-2000 1 01-Mar-2000 2 01-Apr-2000 3 01-May-2000 4 01-Jun-2000 5 01-Jul-2000 6 01-Aug-2000 7 01-Sep-2000 8 01-Oct-2000 9 01-Nov-2000 10 01-Dec-2000 11 01-Jan-2001 12 01-Feb-2001 13 01-Mar-2001 14 01-Apr-2001 15 01-May-2001 16
tt = retime(tt,'monthly','mean')
tt = 100×1 timetable
Time Var1 ___________ ____ 01-Feb-2000 1 01-Mar-2000 2 01-Apr-2000 3 01-May-2000 4 01-Jun-2000 5 01-Jul-2000 6 01-Aug-2000 7 01-Sep-2000 8 01-Oct-2000 9 01-Nov-2000 10 01-Dec-2000 11 01-Jan-2001 12 01-Feb-2001 13 01-Mar-2001 14 01-Apr-2001 15 01-May-2001 16
Then, we can find the list of years covered by our data ...
years = unique(tt.Time.Year,'sorted')
years = 9×1
2000 2001 2002 2003 2004 2005 2006 2007 2008
... and plot them serially ...
figure
hold on
for i = 1:length(years)
ttYear = tt(tt.Time.Year == years(i),:);
plot(ttYear.Time,ttYear.Var1);
end
legend(string(years));
hold off
... or plot them against the months of the year by shifting all the datetime x-values to the same arbitrary year.
figure
hold on
for i = 1:length(years)
ttYear = tt(tt.Time.Year == years(i),:);
ttYear.Time.Year = 2000; % set all datetime x-values to the same arbitrary year
plot(ttYear.Time,ttYear.Var1);
end
xtickformat('MMM');
legend(string(years));
hold off
  4 件のコメント
Seth Furman
Seth Furman 2022 年 4 月 28 日
Do you mean the serial case above (copied below)?
tt = timetable(datetime(2000,1,1)+calmonths(1:100)',(1:100)');
tt = retime(tt,'monthly','mean');
years = unique(tt.Time.Year,'sorted');
f = figure;
hold on
for i = 1:length(years)
ttYear = tt(tt.Time.Year == years(i),:);
plot(ttYear.Time,ttYear.Var1);
end
legend(string(years));
hold off
The colors repeat because, by default, only 7 colors are used. After the 7th line, the colors repeat. We can override this behavior by setting our own colors using the colororder function. When setting the color order, we can take advantage of predefined color maps such as parula and adapt them to the number of lines we have.
co = parula(numel(years));
colororder(f,co);
Lejla Latifovic
Lejla Latifovic 2022 年 4 月 29 日
Not quite like this, I have nine years of half-hourly data (I've converted it to daily and monthly) I want the x-axis to be months, January - December, and all of the years plotted against the montlhy x-axis. I was looking for a way to do this without setting an arbitrary year for all of the years like you have in the code above but it looks like there isn't another way to do this. Thanks for your response!
Thank you for the colour information, I will attempt this.

サインインしてコメントする。

その他の回答 (1 件)

KSSV
KSSV 2021 年 6 月 30 日
Let T be your table.
thedates = T.(1) ;
val = T.(3) ;
[Y,MO,D,H,MI,S] = datevec(thedates) ;
[c,ia,ib] = unique(Y) ;
figure(1)
hold on
for i = 1:length(c)
plot(thedates(ib==i),val(ib==i))
end
legend
  2 件のコメント
Leo Tu
Leo Tu 2021 年 6 月 30 日
Thank you for your response @KSSV. With this script however, I get this plot where the x-axis are labelled in years rather than months and the lines of yearly data are in one line and not overlapping. I have attached the plot for reference. I need the x-axis to be from Jan to Dec and then each yearly data (the coloured lines) to be overlapping. Thank you for your help @KSSV
Leo Tu
Leo Tu 2021 年 6 月 30 日
Sorry @KSSV I made a slight error. when I try using my table (TT) I get an error regarding datevec, stating that the input was was not an array of character vectors or strings. Sorry for the confusion. When I use my orginal table (T) then I get the plot I have shown above.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Preprocessing についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by