How can I plot this date time graph?

23 次查看(过去 30 天)
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
编辑:Voss 2023-2-6
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
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
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
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 个评论
Sulaymon Eshkabilov
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Climate Science and Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by