How can I plot this date time graph?
古いコメントを表示
Hi!
I have this Years.mat timetable where I have three columns - a) date, b) hours of the date, and c) tide height.
Can anyone please help me to know how can I plot a graph where I can show all the months in x-axis and the corresponding heights of those months in the y-axis? Just like this picture -

Any feedback will be much helpful!!
2 件のコメント
Walter Roberson
2023 年 2 月 6 日
I think it would make more sense as a scatter plot than as a line plot.
Ashfaq Ahmed
2023 年 2 月 6 日
採用された回答
その他の回答 (1 件)
load('Years.mat')
whos
Climatology
t = Climatology.TimeSeries + hour(Climatology.HourSeries);
scatter(t, Climatology.HeightSeries)
grid on
xlabel('Time')
ylabel('Tide Height (m)')
I actually think plot looks better than scatter
figure
plot(t, Climatology.HeightSeries)
grid on
xlabel('Time')
ylabel('Tide Height (m)')
8 件のコメント
Ashfaq Ahmed
2023 年 2 月 6 日
load('Years.mat')
t = Climatology.TimeSeries + hour(Climatology.HourSeries);
[y, m, d] = ymd(t);
y(:) = min(y);
hacked_t = datetime(y, m, d);
scatter(hacked_t, Climatology.HeightSeries)
grid on
xlabel('Time')
ylabel('Tide Height (m)')
That is, we hack all of the dates replacing the year with the earliest year, so that (say) May 19th of one year is the same relative offset as May 19th of a different year.
Other approaches have a challenge in trying to get the month label right... unless you want to convert to day of year and use datetick()... and that has challenges with leap years.
load Years
Climatology
Seems like this:
t = Climatology.TimeSeries + hour(Climatology.HourSeries)
Should be this:
t = Climatology.TimeSeries + duration(Climatology.HourSeries,'InputFormat','hh:mm')
Walter Roberson
2023 年 2 月 6 日
My hack-the-year approach does combine across years.
In fact, this is a quite tricky exercise and there are many repetivie dates. So far, no answer gives good sorted or combined time series (@Walter Roberson, @Voss). E.g. Walter's answer:
load('Years.mat')
t = Climatology.TimeSeries + hour(Climatology.HourSeries);
[y, m, d] = ymd(t);
y(:) = min(y);
hacked_t = datetime(y, m, d);
hacked_t(611)
hacked_t(612)
hacked_t(629)
hacked_t(639)
hacked_t(657)
hacked_t(711)
hacked_t(743)
hacked_t(763)
hacked_t(793)
% In some dates, 26 samples, and in others 27 or 28 or .. data points were collected
Climatology.HeightSeries(Climatology.TimeSeries=='31-Jan-2004')
numel(Climatology.HeightSeries(Climatology.TimeSeries=='30-Jan-2004'))
numel(Climatology.HeightSeries(Climatology.TimeSeries=='01-Feb-2004'))
% Therefore, none of the combined and sorted time series is accurate.
% Here is:
plot(hacked_t(Climatology.TimeSeries=='30-Jan-2004'), Climatology.HeightSeries(Climatology.TimeSeries=='30-Jan-2004'))
I assumed that Les's calculation of time was correct, but it had an error. hour() of a duration returns an integer, and when added to a datetime that integer is treated as days unless you specifically convert it to duration
load('Years.mat')
t = Climatology.TimeSeries + hours(hour(Climatology.HourSeries));
[y, m, d] = ymd(t);
y(:) = min(y);
hacked_t = datetime(y, m, d);
scatter(hacked_t, Climatology.HeightSeries)
grid on
xlabel('Time')
ylabel('Tide Height (m)')
Les Beckham
2023 年 2 月 6 日
Good catch, Walter.
Here is a bit more accurately sorted data and averaged data by month.
load('Years.mat')
MONTH_ALL=datetime(Climatology.TimeSeries, 'Format','MMM-uuuu');
CLIMAT = table(MONTH_ALL);
CLIMAT.HS = Climatology.HeightSeries;
plot(CLIMAT.MONTH_ALL, CLIMAT.HS)
t=datetime(2004,01,01):calweeks(4):datetime(2004,12,31);
t = t(2:13);
m = month(t, 'shortname');
%% Separates data by month per year for 2004 to 2021, and computes monthly averages
for ii = 1:12
for jj=1:(2021-2004)
Months{ii, jj} = [m{ii} '-' num2str(2003+jj)];
ANNUAL{ii, jj}=CLIMAT.HS(CLIMAT.MONTH_ALL==Months{ii,jj});
ANNUAL_ave{ii, jj} = mean(ANNUAL{ii,jj});
end
end
%% This part is for plotting separated data by month and by year.
% This part is a bit boring code and not elagant or efficient code due to
% data collection inconsistency by month.
% Plot's xticklabels is not crisp with the data. Some some minor adjustment is left out.
figure('name', 'ALL')
MColor = {'r', 'g', 'b', 'm', 'b', 'c', 'y', 'k', 'r', 'g', 'b', 'm','b', 'c', 'y', 'k','g'};
N=2021-2004;
% January
for jj=1:N
plot(1:numel(ANNUAL{1, jj}),ANNUAL{1, jj}, '*--','Color',MColor{1}), hold on
end
% February
M2 = numel(ANNUAL{1, N});
for jj=1:N
plot(1+M2:numel(ANNUAL{2, jj})+M2,ANNUAL{2, jj},'o-.','Color', MColor{2})
end
% March
M3 = numel(ANNUAL{1, N})+M2;
for jj=1:N
plot(1+M3:numel(ANNUAL{3, jj})+M3,ANNUAL{3, jj},'s-.','Color', MColor{3})
end
% April
M4 = numel(ANNUAL{1, N})+M3;
for jj=1:N
plot(1+M4:numel(ANNUAL{4, jj})+M4,ANNUAL{4, jj},'<-.','Color', MColor{4})
end
% May
M5 = numel(ANNUAL{1, N})+M4;
for jj=1:N
plot(1+M5:numel(ANNUAL{5, jj})+M5,ANNUAL{5, jj},'>-.','Color', MColor{5})
end
% June
M6 = numel(ANNUAL{1, N})+M5;
for jj=1:N
plot(1+M6:numel(ANNUAL{6, jj})+M6,ANNUAL{6, jj},'p-.','Color', MColor{6})
end
% July
M7 = numel(ANNUAL{1, N})+M6;
for jj=1:N
plot(1+M7:numel(ANNUAL{7, jj})+M7,ANNUAL{7, jj},'p-.','Color', MColor{7})
end
% August
M8 = numel(ANNUAL{1, N})+M7;
for jj=1:N
plot(1+M8:numel(ANNUAL{8, jj})+M8,ANNUAL{8, jj},'p-.','Color', MColor{8})
end
% September
M9 = numel(ANNUAL{1, N})+M8;
for jj=1:N
plot(1+M9:numel(ANNUAL{9, jj})+M9,ANNUAL{9, jj},'p-.','Color', MColor{9})
end
% October
M10 = numel(ANNUAL{1, N})+M9;
for jj=1:N
plot(1+M10:numel(ANNUAL{10, jj})+M10,ANNUAL{10, jj},'p-.','Color', MColor{10})
end
% November
M11 = numel(ANNUAL{1, N})+M10;
for jj=1:N
plot(1+M11:numel(ANNUAL{11, jj})+M11,ANNUAL{11, jj},'p-.','Color', MColor{11})
end
% December
M12 = numel(ANNUAL{1, N})+M11;
for jj=1:N
plot(1+M12:numel(ANNUAL{12, jj})+M12,ANNUAL{12, jj},'p-.','Color', MColor{12})
end
xticks(linspace(1, numel(ANNUAL{12, N})+M12, 12))
xticklabels({'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'})
xtickangle(60)
xlim([1, numel(ANNUAL{12, N})+M12])
xlabel('Date Time by month for 2004 - 2021')
ylabel('Tide Height, (m)')
title('Monthly variation of tide height (2004-2021)')
hold off
%% Averaged per month for 2004 - 2021
figure('name', 'ALL')
MType = {'*', 'o', 's', '<', '>', 'd', 'p', 'h', 's', 'h', 'x', '^'};
MColor ={'r', 'g', 'b', 'm', 'b', 'g', 'b', 'k', 'r', 'k', 'b', 'm'};
N=2021-2004;
for k=1:12
AVE=cell2mat(ANNUAL_ave(k,1:17));
plot(k*ones(1,N),AVE, MType{k},'Color',MColor{k}, 'MarkerFaceColor', 'y'), hold on
end
xlim([0,13])
xticks(1:12)
xticklabels({'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'})
xtickangle(45)
xlabel('Date Time by month for 2004 - 2021')
ylabel('Tide Height, (m)')
title('Monthly averaged variation of tide height (2004-2021)')
grid on
hold off
カテゴリ
ヘルプ センター および File Exchange で Climate Science and Analysis についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!








