How to plot 2 years of timetable data on top of each other with the 1 year on the x-axis?
11 次查看(过去 30 天)
显示 更早的评论
Hi, i am trying to plot 2 years of timetable data with hourly soil moisture values from 2016 and 2017. I want to be able to plot them on top of each other to compare their trends. For ease, i have already removed the leap day in 2016 so both years have the same number of data points. I have managed to obtain a plot like this, however, the dates on the x-axis specifically relates to the 2016 data set (this of course is the right scale but i want to be able to have a generalised year scale on the x-axis instead). I would prefer the scale to be in the format of months and days i.e dd/mm. Any help would be greatly appreciated. My current code and figure is as follows:![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/891340/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/891340/image.jpeg)
% find start and end rows - HOURLY
start_2016 = find(year(TT.DT)==2016 & month(TT.DT)==1 & day(TT.DT)==1 & hour(TT.DT)==0);
end_2016 = find(year(TT.DT)==2016 & month(TT.DT)==12 & day(TT.DT)==31 & hour(TT.DT)==23);
start_2017 = find(year(TT.DT)==2017 & month(TT.DT)==1 & day(TT.DT)==1 & hour(TT.DT)==0);
end_2017 = find(year(TT.DT)==2017 & month(TT.DT)==12 & day(TT.DT)==31 & hour(TT.DT)==23);
% obtain data for each year (2016 and 2017) - HOURLY
y_2016 = TT.DT(start_2016: end_2016);
y_2017 = TT.DT(start_2017: end_2017);
y_2016_sm = TT.SM(start_2016: end_2016);
y_2017_sm = TT.SM(start_2017: end_2017);
% plot graphs
figure(1)
subplot(2,1,1)
title('Hourly')
plot(y_2016,y_2016_sm)
hold on
plot(y_2016,y_2017_sm)
ylabel('Volumetric Soil Moisture')
grid on
grid minor
legend('2016','2017')
subplot(2,1,2)
title('Hourly')
plot(y_2016_sm,'.')
hold on
plot(y_2017_sm,'.')
ylabel('Volumetric Soil Moisture')
grid on
grid minor
legend('2016','2017')
0 个评论
回答(1 个)
Adam Danz
2022-2-11
One simple approach would be to change the year for all of the data to equal 2016 or any other leap year (see leapyear function). The plot the data using datetime values so you don't have to get rid of the leap days. You can specify the datetime format in the x-tick labels by setting TickLabelFormat.
ax.XAxis.TickLabelFormat = 'mmm'; % ax is axis handle
To change the year of datetime values,
dt.Year = 2016; % dt is the datetime vector
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!