How Timelines Unify in subplot

2 views (last 30 days)
peter huang
peter huang on 24 Sep 2022
Edited: dpb on 25 Sep 2022
I want to unify the time of the xtick in the subplot, but the time length of the data is different I adjusted through the axis but the graphs that have not yet reached 2021 cannot appear in 2021
test.m code:
clear all ; clc ; clf
load compound_tide_1516_data_wl_3.mat
load compound_tide_1516_data_wl_5.mat
subplot(211)
plot(time_point_1516_wl_3 ,moving_M2_ht_wl_3,'r--*' );hold on
plot(time_point_1516_wl_3 ,moving_S2_ht_wl_3,'b--*' );hold on
plot(time_point_1516_wl_3 ,moving_K1_ht_wl_3,'c--*' );hold on
plot(time_point_1516_wl_3 ,moving_O1_ht_wl_3,'m--*' );hold on
datetick('x','yyyy')
axis([time_point_1516_wl_3(1) time_point_1516_wl_3(end)+200 0.0 0.30])
text1 = text(time_point_1516_wl_3(1)+100,0.27,'WL:3 yr')
% axis([time_point_1516_wl_3(1) time_point_1516_wl_3(end)+600 0.0 0.30])
set(text1,'FontSize',15)
set(gca,'FontSize',15)
subplot(212)
plot(time_point_1516_wl_5 ,moving_SA_ht_wl_5,'k--*' );hold on
plot(time_point_1516_wl_5 ,moving_M2_ht_wl_5,'r--*' );hold on
plot(time_point_1516_wl_5 ,moving_S2_ht_wl_5,'b--*' );hold on
plot(time_point_1516_wl_5 ,moving_K1_ht_wl_5,'c--*' );hold on
plot(time_point_1516_wl_5 ,moving_O1_ht_wl_5,'m--*' );hold on
% datetick('x',28)
% axis([time_point_1516_wl_3(1) time_point_1516_wl_3(end) 0.0 0.30])
datetick('x','yyyy')
text2 = text(time_point_1516_wl_3(1)+100,0.27,'WL:5 yr')
set(text2,'FontSize',15)
set(gca,'FontSize',15)
  3 Comments
dpb
dpb on 25 Sep 2022
Edited: dpb on 25 Sep 2022
"...I want to display 2008-2022 in both..."
Then set xlim to the desired range in each...it is what controls the range of the x-axis.
In the example code below where I dynamically set the upper range to the first day of the following year of the data in the graph, you could do something like (presuming you had both datasets as intended, not just the same data for both)
xlim([datetime(min(year(T3(1)),year(T5(1))),1,1),datetime(max(year(T3(1)),year(T5(1)))+1,1,1])
which would return the min and max years between the two datasets and make the same adustment to the beginning of first and subsequent years. Or, of course, you can always hardcode in exact limits wanted if there are specific points in time of interest, not just the overall range of the data and to make even/same axes on each plot. You might also want to explore <linkaxes> to put the two in synch with each other if you choose to interactively zoom or somesuch.
"...why I should use datetime instead"
The short answer is "just because!". <VBG> While tongue-in-cheek quip, the features and comparative ease of use of the datetime class as compared to datenum are simply not to be ignored (besides the note in the documentation from TMW that reads <datenum "(Not recommended; use datetime or duration)">. In particular, for the specific use here, the plot() axes are now datetime aware and you can thus get rid of the terrible, klunky now outdated datetick routine entirely and set formats, dateticks, limits, etc.,etc., etc., ... directly with datetime values instead. Yes, you can set xlim with datenum with an analogous call to datenum passing year,month,day, but the supporting routines for the venerable datenum are not as convenient in many cases and even if are for a particular case, you're still stuck with datetick which is truly to be a thing to be glad to be able to do away with.
That doesn't even begin to touch the use in timetable and the like...or the fact that TMW has relegated the venerable datenum time storage mechanism to the dustbin and it will continue to get more and more out-of-date with each and every newer release. It won't ever be completely dropped because it's been around for too long and would cause too much grief in existing code, but shouldn't be used for new code unless there's some truly pressing outside need.
Besides the coding benefits, with datetime you get a display format for free along with it; no more need to convert to "real" time with datestr to see if what you have is what you think you got...the display format at the command line and in tables, etc., etc., is already there in readable date/time units (that you can change to display in your preferred format for the particular use/place at will). You simply cannot do that with a datenum
fn2=websave('compound_tide_1516_data_wl_5.mat','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1134780/compound_tide_1516_data_wl_5.mat');
whos('-file',fn2)
Name Size Bytes Class Attributes moving_K1_ht_wl_3 1x5 40 double moving_M2_ht_wl_3 1x5 40 double moving_M4_ht_wl_3 1x5 40 double moving_O1_ht_wl_3 1x5 40 double moving_S2_ht_wl_3 1x5 40 double moving_SA_ht_wl_3 1x5 40 double time_point_1516_wl_3 1x5 40 double
The second .mat file still contains the same 3-yr data as the first...so can't illustrate w/o taking the time to make up sample data...

Sign in to comment.

Answers (1)

dpb
dpb on 24 Sep 2022
fn1=websave('compound_tide_1516_data_wl_3.mat','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1134780/compound_tide_1516_data_wl_3.mat');
fn1
fn1 = '/users/mss.system.xVSnbh/compound_tide_1516_data_wl_3.mat'
whos('-file',fn1)
Name Size Bytes Class Attributes moving_K1_ht_wl_3 1x5 40 double moving_M2_ht_wl_3 1x5 40 double moving_M4_ht_wl_3 1x5 40 double moving_O1_ht_wl_3 1x5 40 double moving_S2_ht_wl_3 1x5 40 double moving_SA_ht_wl_3 1x5 40 double time_point_1516_wl_3 1x5 40 double
load(fn1)
fn2=websave('compound_tide_1516_data_wl_5.mat','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1134780/compound_tide_1516_data_wl_5.mat');
fn2
fn2 = '/users/mss.system.xVSnbh/compound_tide_1516_data_wl_5.mat'
whos('-file',fn2)
Name Size Bytes Class Attributes moving_K1_ht_wl_3 1x5 40 double moving_M2_ht_wl_3 1x5 40 double moving_M4_ht_wl_3 1x5 40 double moving_O1_ht_wl_3 1x5 40 double moving_S2_ht_wl_3 1x5 40 double moving_SA_ht_wl_3 1x5 40 double time_point_1516_wl_3 1x5 40 double
%return
%load(fn2)
clear fn*
T3=datetime(time_point_1516_wl_3,'ConvertFrom','datenum');
W3=[moving_M2_ht_wl_3;
moving_S2_ht_wl_3;
moving_K1_ht_wl_3;
moving_O1_ht_wl_3].';
% T5=datetime(time_point_1516_wl_5,'ConvertFrom','datenum');
% W5=[moving_M2_ht_wl_5;
% moving_S2_ht_wl_5;
% moving_K1_ht_wl_5;
% moving_O1_ht_wl_5;
% moving_SA_ht_wl_5].';
C={'r';'b';'c';'m';'k'}; % colors; can't use multiple named colors in 'color'
hAx(1)=subplot(2,1,1);
hL(1,:)=plot(T3,W3,'--*');
xlim([datetime(year(T3(1)),1,1),datetime(year(T3(end))+1,1,1)])
set(hL(1,:),{'Color'},C(1:numel(hL(1,:))));
title('WL:3 yr')
hAx(2)=subplot(2,1,2);
hL(2,:)=plot(T5,W5,'linestyle','--*');
Unrecognized function or variable 'T5'.
xlim([datetime(year(T5(1)),1,1),datetime(year(T5(end))+1,1,1)])
set(hL(2,:),{'Color'},C(1:numel(hL(2,:))));
title('WL:5 yr')
Recasts into more compact array form instead of all the terribly-long-and-verbose-variable-names and recasts the time data into datetime. I also then expanded the axes limits to the beginning the year of the first time point and then to the next year after the last year.
NB: The data in the two files is the same 3-YR data so can't do anything about the 5-yr data to see what the underlying Q? really was...if the above doesn't provide sufficient hints on progressing, replace the 5-yr data .mat file with the correct data and explain the problem more precisely.

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!