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
Walter Roberson 2023 年 2 月 6 日
I think it would make more sense as a scatter plot than as a line plot.
Ashfaq Ahmed
Ashfaq Ahmed 2023 年 2 月 6 日
Yes, I agree. Can you please give me an idea on the scatter plot of this problem?

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

 採用された回答

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023 年 2 月 6 日
編集済み: Voss 2023 年 2 月 6 日

1 投票

Here is how you can get the plot:
load('Years.mat')
N = numel(Climatology.HeightSeries);
plot(1:N,Climatology.HeightSeries), shg
xticks(linspace(1, N, 12));
xticklabels({'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'})
xlabel('Date Time')
ylabel('Tide Height, (m)')
title('Monthly variation of tide height (2004-2021)')
[Edit to run the code and show the plot. -Voss]

4 件のコメント

Ashfaq Ahmed
Ashfaq Ahmed 2023 年 2 月 6 日
excellent!
Voss
Voss 2023 年 2 月 6 日
編集済み: Voss 2023 年 2 月 6 日
@Ashfaq Ahmed: Note that the plot generated by this answer is the same as the plot generated by the other answer; this answer merely labels the x-axis with the names of the months, which is misleading.
For example:
  • the x-tick labeled 'Jan' here actually corresponds to data from the beginning of 2004
  • the x-tick labeled 'May' here actually corresponds to data from around 2009-2010
  • the x-tick labeled 'Sep' here actually corresponds to data from around 2015-2016
  • the x-tick labeled 'Dec' here actually corresponds to data from the beginning of 2022
  • etc.
I don't think this is the result you're after.
Ashfaq Ahmed
Ashfaq Ahmed 2023 年 2 月 6 日
Oh yes. You are right!! Thank you so much for the check. I was about to put the plot in my paper.
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023 年 2 月 6 日
編集済み: Sulaymon Eshkabilov 2023 年 2 月 6 日
Here some addional coding is required.
Step (1). Sort data by all the dates (only data collected ones) and combine the data by dates
Step (2) Average the values per date for every day (data collected ones) of each month of the year
Step (3) Then plot the data by dates (month) per year

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

その他の回答 (1 件)

Les Beckham
Les Beckham 2023 年 2 月 6 日

0 投票

load('Years.mat')
whos
Name Size Bytes Class Attributes Climatology 180468x2 23462081 timetable ans 1x34 68 char cmdout 1x33 66 char
Climatology
Climatology = 180468×2 timetable
TimeSeries HourSeries HeightSeries ___________ __________ ____________ 01-Jan-2004 {'00:00'} 0.207 01-Jan-2004 {'01:00'} 0.189 01-Jan-2004 {'01:12'} 0.226 01-Jan-2004 {'02:00'} 0.263 01-Jan-2004 {'03:00'} 0.318 01-Jan-2004 {'04:00'} 0.394 01-Jan-2004 {'05:00'} 0.58 01-Jan-2004 {'06:00'} 0.843 01-Jan-2004 {'07:00'} 1.065 01-Jan-2004 {'08:00'} 1.149 01-Jan-2004 {'08:12'} 1.1155 01-Jan-2004 {'09:00'} 1.082 01-Jan-2004 {'10:00'} 0.918 01-Jan-2004 {'11:00'} 0.693 01-Jan-2004 {'12:00'} 0.458 01-Jan-2004 {'13:00'} 0.322
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
Ashfaq Ahmed 2023 年 2 月 6 日
Hi! Thank you so much! But I actually want to keep the months in the x-axis. Not the year!
Walter Roberson
Walter Roberson 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.
Voss
Voss 2023 年 2 月 6 日
load Years
Climatology
Climatology = 180468×2 timetable
TimeSeries HourSeries HeightSeries ___________ __________ ____________ 01-Jan-2004 {'00:00'} 0.207 01-Jan-2004 {'01:00'} 0.189 01-Jan-2004 {'01:12'} 0.226 01-Jan-2004 {'02:00'} 0.263 01-Jan-2004 {'03:00'} 0.318 01-Jan-2004 {'04:00'} 0.394 01-Jan-2004 {'05:00'} 0.58 01-Jan-2004 {'06:00'} 0.843 01-Jan-2004 {'07:00'} 1.065 01-Jan-2004 {'08:00'} 1.149 01-Jan-2004 {'08:12'} 1.1155 01-Jan-2004 {'09:00'} 1.082 01-Jan-2004 {'10:00'} 0.918 01-Jan-2004 {'11:00'} 0.693 01-Jan-2004 {'12:00'} 0.458 01-Jan-2004 {'13:00'} 0.322
Seems like this:
t = Climatology.TimeSeries + hour(Climatology.HourSeries)
t = 180468×1 datetime array
01-Jan-2004 02-Jan-2004 02-Jan-2004 03-Jan-2004 04-Jan-2004 05-Jan-2004 06-Jan-2004 07-Jan-2004 08-Jan-2004 09-Jan-2004 09-Jan-2004 10-Jan-2004 11-Jan-2004 12-Jan-2004 13-Jan-2004 14-Jan-2004 14-Jan-2004 15-Jan-2004 16-Jan-2004 17-Jan-2004 18-Jan-2004 19-Jan-2004 20-Jan-2004 21-Jan-2004 21-Jan-2004 22-Jan-2004 23-Jan-2004 24-Jan-2004 02-Jan-2004 03-Jan-2004
Should be this:
t = Climatology.TimeSeries + duration(Climatology.HourSeries,'InputFormat','hh:mm')
t = 180468×1 datetime array
01-Jan-2004 00:00:00 01-Jan-2004 01:00:00 01-Jan-2004 01:12:00 01-Jan-2004 02:00:00 01-Jan-2004 03:00:00 01-Jan-2004 04:00:00 01-Jan-2004 05:00:00 01-Jan-2004 06:00:00 01-Jan-2004 07:00:00 01-Jan-2004 08:00:00 01-Jan-2004 08:12:00 01-Jan-2004 09:00:00 01-Jan-2004 10:00:00 01-Jan-2004 11:00:00 01-Jan-2004 12:00:00 01-Jan-2004 13:00:00 01-Jan-2004 13:48:00 01-Jan-2004 14:00:00 01-Jan-2004 15:00:00 01-Jan-2004 16:00:00 01-Jan-2004 17:00:00 01-Jan-2004 18:00:00 01-Jan-2004 19:00:00 01-Jan-2004 20:00:00 01-Jan-2004 20:48:00 01-Jan-2004 21:00:00 01-Jan-2004 22:00:00 01-Jan-2004 23:00:00 02-Jan-2004 00:00:00 02-Jan-2004 01:00:00
Walter Roberson
Walter Roberson 2023 年 2 月 6 日
@Voss is correct that @Sulaymon Eshkabilov and @Les Beckham do not combine data across years.
My hack-the-year approach does combine across years.
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023 年 2 月 6 日
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)
ans = datetime
31-Jan-2004
hacked_t(612)
ans = datetime
01-Feb-2004
hacked_t(629)
ans = datetime
24-Jan-2004
hacked_t(639)
ans = datetime
01-Feb-2004
hacked_t(657)
ans = datetime
25-Jan-2004
hacked_t(711)
ans = datetime
27-Jan-2004
hacked_t(743)
ans = datetime
01-Feb-2004
hacked_t(763)
ans = datetime
18-Feb-2004
hacked_t(793)
ans = datetime
30-Jan-2004
% In some dates, 26 samples, and in others 27 or 28 or .. data points were collected
Climatology.HeightSeries(Climatology.TimeSeries=='31-Jan-2004')
ans = 27×1
0.2390 0.1930 0.2850 0.3940 0.4790 0.6110 0.8020 0.9660 0.9975 1.0290
numel(Climatology.HeightSeries(Climatology.TimeSeries=='30-Jan-2004'))
ans = 27
numel(Climatology.HeightSeries(Climatology.TimeSeries=='01-Feb-2004'))
ans = 26
% 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'))
Walter Roberson
Walter Roberson 2023 年 2 月 6 日
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
Les Beckham 2023 年 2 月 6 日
Good catch, Walter.
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023 年 2 月 7 日
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 ExchangeClimate Science and Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by