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

14 次查看(过去 30 天)
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
linkaxes(h,'x')
  1 个评论
dpb
dpb 2018-4-24
编辑:dpb 2018-4-24

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
>> 

请先登录,再进行评论。

回答(1 个)

Doug
Doug 2022-3-17
I found that if you set:
valuesTs.TimeInfo.Format='MMM dd'
It will plot your x-axis labels as desired when plotting the timeseries but also throws an error. Not sure why the error.
  2 个评论
Alan Bindemann
Alan Bindemann 2022-3-21
The error is becuase 'MMM' is an invalid format for minutes 'mmm' should be used for months. Its interesting that this places the part of the date below the x-ticks, however, the timeseries method isn't smart enough to move the "common" portion of the ticks as you zoom in and out, which is what I like about the datetime approach.
Doug
Doug 2022-3-21
My Matlab documentation says MMM is Month, abbreviated name. Here's a snip:
When I zoom the x-axis of the timeseries plot I do get the behaviour where the common portion of the ticks move below and to the right. See below. I believe this is what you were looking for, correct? I'm using Matlab version R2021b if that matters.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Time Series 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by