Questions about "dseries" plot method behaviour

Hi everyone,

I’m working with dseries class, but when I try to plot an object (with the overloaded plot function), dates won’t be displayed in the x-axis but rather the default XTicks:

% dseries - MWE 1
clear, clc, close all;
rng(1234)

qq = dates('Q'); % Instantiate qq object
xData = randn(100,1);
ts = dseries(xData,qq(2000,1),{'myVar'});

figure;
plot(ts,'k');
print('mwe_1','-dpng')

Furthermore, when trying to plot two series, rather than sharing the same x-axis and having two lines, they get mapped into each other like a scatter plot:

% dseries - MWE 2
clear, clc, close all;
rng(1234)

qq = dates('Q'); % Instantiate qq object
xData1 = randn(100,1);
xData2 = randn(100,1);
ts = dseries([xData1 xData2],qq(2000,1),{'myVar1','myVar2'});

figure;
plot(ts.myVar1,ts.myVar2);
print('mwe_2','-dpng')

I have two questions:

  1. In the first example, Shouldn’t the plot function handle the labeling? I saw this post when it’s said that dates being displayed should be the expected behavior. Taking a look at the function’s code (located at matlab/dseries/src/@dseries/plot.m), I don’t really understand the section which handles this matter:
function h = plot(o, varargin)

% Overloads Matlab/Octave's plot function for dseries objects.

% Copyright © 2013-2017 Dynare Team

...

switch ndseries
  case 1
    if isequal(nvariables,1)
        hh = plot(o.data,varargin{:});
    else
        ...
        hh = plot(o.data);
    end
    axis tight;
    id = get(gca,'XTick');
    if isequal(id(1),0)
        dates = strings([o.dates(1)-1,o.dates(id(2:end))]);
        skip_relabeling=0;
    else
        ID = id(find(isint(id)));
        if any(ID<o.dates(1).double) || any(ID>o.dates(end).double)
            skip_relabeling=1; %hold on was used so that axis tight did not restrict current xlim to dseries used.
        else
            set(gca,'XTick',ID);
            dates = strings(o.dates(ID));
            skip_relabeling=0;
        end
    end
    if ~skip_relabeling
        set(gca,'XTickLabel',dates);
    end
  case 2
    ...
  otherwise
    error('dseries::plot: This is a bug! Please report the bug to the authors of Dynare.')
end

Just after axis tight the code gets the XTick and tries to check if the first element is zero (which doesn’t happen in the MWE 1, it starts at 10), since this isn’t the case, it jumps at the else block where an additional ID variable is created (I thinks it’s used when plotting monthly data), then the statement any(ID<o.dates(1).double) || any(ID>o.dates(end).double) is tested, where I don’t quite get when that would be false, and I think this is the reason dates aren’t displayed, because skip_relabeling is set to 0, skipping entirely the labeling.

  1. In the second example, looking at the code it seems that the default behaviour would be to plot two series (ts.myVar1 and ts.myVar2) with a common x-axis (handled by the align method), but the only way I’m getting that result is by either passing only one dseries object (e.g. plot(ts)), or concatenating them (e.g. plot([ts.myVar1 ts.myVar2])) or plotting separetely using hold on, which I think it defeates the purpose of overloading plot to have a more convenient way to graphically display time series without the burden of setting the format by hand. Is this the expected result or is some kind of bug?

Thanks in advance for your help!
S.

PD: I’m running Dynare 6.2 with MATLAB R2024a.

There was a previous example by Stéphane Adjemian (see Placing dates on the horizontal axis of historical shock decomposition shocks - #4 by stepan-a) showing its very easy implementation.

However, for me the above example does not work in Dyanre 6. The answer can be found in the examples in

If you have created two dseries objects named, say, ts1 and ts2 (with the latter being in a subsample of the former)

plot(ts1(ts2.dates).data,‘-k’); % Plot of the stationary component.
hold on
plot(ts2.data,‘–r’); % Plot of the filtered y.
hold off
axis tight
id = get(gca,‘XTick’);
set(gca,‘XTickLabel’,strings(ts1.dates(id)));

The key for the dates being plotted lies in the last two lines.

Thank you very much for reporting this. This is a bug, see No dates in Matlab plot when plotting (#59) · Issues · Dynare / dseries · GitLab. The internal logic is strange. @stepan-a may want to have a look.

Hi, thaks for your response.

I’m aware of those examples in the Reference Manual that plot dseries and the workaround you describe in the last two lines. However, as I mention above, it shouldn’t be necessary to fix the XTickLabel manually. I tried with an older version (5.4) and still it doesn’t work. For now I guess we have to stick to setting XTickLabel manually.

Thanks professor Pfeifer.

I see that Issue #59 is related to my first question, by any chance the behaviour displayed in my second question (MWE 2) is the desired one (i.e. passing two dseries to plot() and getting a relation between these two rather than displaying two series with a common x-axis) or also a bug?

Thanks!

That is documented, i.e. expected behavior. As the manual states:

If two dseries objects, A and B, are passed as input arguments, the plot function will plot the variables in A against the variables in B

1 Like