createGraph.m 3.07 KB
Newer Older
Houtan Bastani's avatar
Houtan Bastani committed
1
2
3
4
5
function o = createGraph(o)
%function o = createGraph(o)
% Create the graph
%
% INPUTS
Houtan Bastani's avatar
Houtan Bastani committed
6
%   o   [graph] graph object
Houtan Bastani's avatar
Houtan Bastani committed
7
8
%
% OUTPUTS
Houtan Bastani's avatar
Houtan Bastani committed
9
%   o   [graph] graph object
Houtan Bastani's avatar
Houtan Bastani committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
%
% SPECIAL REQUIREMENTS
%   none

% Copyright (C) 2013 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.

if ~isempty(o.figname)
32
33
    warning('@graph.createGraph: will overwrite %s with new graph\n', ...
            o.figname);
Houtan Bastani's avatar
Houtan Bastani committed
34
35
end

36
37
38
39
40
if ~o.seriesElements.numElements()
    warning('@graph.crepateGraph: no series to plot, returning');
    return;
end

Houtan Bastani's avatar
Houtan Bastani committed
41
disp('creating plot..........');
Houtan Bastani's avatar
Houtan Bastani committed
42
43
44
h = figure('visible','off');
hold on;
box on;
Houtan Bastani's avatar
Houtan Bastani committed
45
46
47
48
if o.grid
    grid on;
    set(gca, 'GridLineStyle', '--');
end
Houtan Bastani's avatar
Houtan Bastani committed
49

50
51
52
53
54
55
if isempty(o.xrange)
    dd = o.seriesElements.getMaxRange();
else
    dd = o.xrange;
end

56
ne = o.seriesElements.numElements();
57
for i=1:ne
58
    o.seriesElements(i).getLine(dd);
Houtan Bastani's avatar
Houtan Bastani committed
59
60
end

61
62
x = 1:1:dd.ndat;
xlabels = getDatesCellStringArray(dd);
Houtan Bastani's avatar
Houtan Bastani committed
63

64
if ~isempty(o.yrange)
65
    ylim(o.yrange);
Houtan Bastani's avatar
Houtan Bastani committed
66
67
end

Houtan Bastani's avatar
Houtan Bastani committed
68
if ~isempty(o.shade)
69
70
    x1 = find(strcmpi(o.shade(1).format(), xlabels));
    x2 = find(strcmpi(o.shade(o.shade.ndat).format(), xlabels));
Houtan Bastani's avatar
Houtan Bastani committed
71
    assert(~isempty(x1) && ~isempty(x2), ['@graph.createGraph: either ' ...
72
                        o.shade(1).format() ' or ' o.shade(o.shade.ndat).format() ' is not in the date ' ...
Houtan Bastani's avatar
Houtan Bastani committed
73
                        'range of data selected.']);
Houtan Bastani's avatar
Houtan Bastani committed
74
75
76
77
    yrange = get(gca, 'YLim');

    % From ShadePlotForEmpahsis (Matlab Exchange)
    % use patch bc area doesn't work with matlab2tikz
Houtan Bastani's avatar
Houtan Bastani committed
78
79
    patch([repmat(x1, 1, 2) repmat(x2, 1, 2)], ...
          [yrange fliplr(yrange)], 'b', 'FaceAlpha', .2);
Houtan Bastani's avatar
Houtan Bastani committed
80
81
82
83
84
85
end

set(gca,'XTick', x);
set(gca,'XTickLabel', xlabels);

if o.legend
86
    lh = legend(o.seriesElements.getNames());
Houtan Bastani's avatar
Houtan Bastani committed
87
88
89
90
91
92
93
    set(lh, 'orientation', o.legend_orientation);
    set(lh, 'Location', o.legend_location);
    set(lh, 'FontSize', o.legend_font_size);
    legend('boxoff');
end

if ~isempty(o.xlabel)
Houtan Bastani's avatar
Houtan Bastani committed
94
    xlabel(['$\textbf{\footnotesize ' o.xlabel '}$'], 'Interpreter', 'LaTex');
Houtan Bastani's avatar
Houtan Bastani committed
95
96
97
end

if ~isempty(o.ylabel)
Houtan Bastani's avatar
Houtan Bastani committed
98
    ylabel(['$\textbf{\footnotesize ' o.ylabel '}$'], 'Interpreter', 'LaTex');
Houtan Bastani's avatar
Houtan Bastani committed
99
100
101
end

if ~isempty(o.title)
Houtan Bastani's avatar
Houtan Bastani committed
102
    title( o.title, 'Interpreter', 'LaTex');
Houtan Bastani's avatar
Houtan Bastani committed
103
end
104
105
106
107
108
109
110

if o.zeroline
    a = ylim;
    if 0 > a(1) && 0 < a(2)
        plot(xlim, [0 0], 'color', 'k', 'LineWidth', 0.25);
    end
end
Houtan Bastani's avatar
Houtan Bastani committed
111
112
drawnow;

113
o.figname = [tempname '.tex'];
Houtan Bastani's avatar
Houtan Bastani committed
114
disp('  converting to tex....');
Houtan Bastani's avatar
Houtan Bastani committed
115
116
117
118
119
matlab2tikz('filename', o.figname, ...
            'showInfo', false, ...
            'showWarnings', false, ...
            'checkForUpdates', false);

Houtan Bastani's avatar
Houtan Bastani committed
120
grid off;
Houtan Bastani's avatar
Houtan Bastani committed
121
122
123
124
125
box off;
hold off;
close(h);
clear h;
end