How can I plot this date time graph?
33 次查看(过去 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 -
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1286235/image.png)
Any feedback will be much helpful!!
2 个评论
采纳的回答
Sulaymon Eshkabilov
2023-2-6
编辑: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 个评论
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
2023-2-6
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 个评论
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Oceanography and Hydrology 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!