## How do I make the x-axis of plotted timeseries data behave like it does when using datetime values?

### Alan Bindemann (view profile)

on 24 Apr 2018
Latest activity Edited by dpb

### dpb (view profile)

on 24 Apr 2018
I much prefer the zooming behavior of plots created using datetime values to that seen when you plot timeseries data that uses the TimeInfo.StartDate feature. The sample code below illustrates the two approaches. When I zoom in, the bottom axis that uses datetime is much smarter about how times are displayed, offsetting year, month and day to the lower right-hand corner when appropriate.
I have a Simulink model that generates output using timeseries objects and would like the plot method on these objects to have behavior that matches datetime.
I've looked at the properties of each axis but cannot find any significant differences between them. Any suggestions on how to make this happen would be appreciated.
% Vector of 20 days at 1 minute intervals using MATLAB datenums
datesDatenum = (737342:(1/1440):737362)';
% Elapsed time in seconds
times_sec = (datesDatenum - datesDatenum(1))*86400;
% Sample values to plot
values = sind(1:numel(datesDatenum))';
% Create a timeseries
valuesTs = timeseries(values, times_sec);
valuesTs.TimeInfo.StartDate = datestr(datesDatenum(1));
valuesTs.TimeInfo.Units = 'seconds';
% Create dates using datetime
times_dt = datetime(datesDatenum,'ConvertFrom','datenum');
figure; % Create figure and link axes
% Plot as timeseries
h(1) = subplot(2,1,1);
plot(valuesTs); grid on
% Plot using datetime
h(2) = subplot(2,1,2);
plot(times_dt, values);grid on

dpb

### dpb (view profile)

on 24 Apr 2018

Those behavior differences are inherent in the implementation of the two functions; the @timeseries version of plot.m contains some code to generate a "local" Time vector and sets some internal formatting before it calls the @datetime version. You can dig into those internals and see if can ascertain which might possibly be tweaked in a special version but it's pretty deep in the bowels of how TMW has implemented HG2 as to what is actually different and undocumented so good luck.

I'd venture if you want datetime plotting behavior, use and PLOT() a datetime vector will be the only practical way without spending buku's of time trying to figure it out and even then never getting the desired behavior.

```>> which plot -all
built-in (C:\ML_R2017\toolbox\matlab\graph2d\plot)
C:\ML_R2017\toolbox\matlab\polyfun\@polyshape\plot.m      % polyshape method
C:\ML_R2017\toolbox\matlab\polyfun\@alphaShape\plot.m     % alphaShape method
C:\ML_R2017\toolbox\matlab\bigdata\@tall\plot.m           % tall method
C:\ML_R2017\toolbox\matlab\graphfun\@graph\plot.m         % graph method
C:\ML_R2017\toolbox\matlab\graphfun\@digraph\plot.m       % digraph method
C:\ML_R2017\toolbox\curvefit\curvefit\@sfit\plot.m        % sfit method
C:\ML_R2017\toolbox\curvefit\curvefit\@cfit\plot.m        % cfit method
C:\ML_R2017\toolbox\signal\signal\@dspdata\plot.m         % dspdata method
C:\ML_R2017\toolbox\stats\classreg\@LinearModel\plot.m    % LinearModel method
C:\ML_R2017\toolbox\matlab\timeseries\@timeseries\plot.m  % timeseries method
>>
```