diff --git a/Figure/RemovePlotWhiteArea.m b/Figure/RemovePlotWhiteArea.m new file mode 100644 index 0000000..fa7c3bb --- /dev/null +++ b/Figure/RemovePlotWhiteArea.m @@ -0,0 +1,26 @@ +% RemovePlotWhiteArea: 去除Plot画的图的空白部分 +% usage:add RemovePlotWhiteArea(gca) immediately after you complete your plot +% 输入 +% gca: Axes handle + +% author : songxf +% time : 2020-03-06 +% email : + +function [] = RemovePlotWhiteArea(gca) +% TightInset的位置 +inset_vectior = get(gca, 'TightInset'); +inset_x = inset_vectior(1); +inset_y = inset_vectior(2); +inset_w = inset_vectior(3); +inset_h = inset_vectior(4); + +% OuterPosition的位置 +outer_vector = get(gca, 'OuterPosition'); +pos_new_x = outer_vector(1) + inset_x+0.01; % 将Position的原点移到到TightInset的原点 +pos_new_y = outer_vector(2) + inset_y+0.01; +pos_new_w = outer_vector(3) - inset_w - inset_x-0.02; % 重设Position的宽 +pos_new_h = outer_vector(4) - inset_h - inset_y-0.02; % 重设Position的高 + +% 重设Position +set(gca, 'Position', [pos_new_x, pos_new_y, pos_new_w, pos_new_h]); diff --git a/Figure/RemoveSubplotWhiteArea.m b/Figure/RemoveSubplotWhiteArea.m new file mode 100644 index 0000000..8b2ee36 --- /dev/null +++ b/Figure/RemoveSubplotWhiteArea.m @@ -0,0 +1,40 @@ +% RemoveSubplotWhiteArea: 去除subplot周围的空白部分 +% usage:RemoveSubplotWhiteArea(gca, sub_row, sub_col, current_row, current_col) +% 输入 +% gca :axes句柄 +% sub_row :subplot的行数 +% sub_col :subplot的列数 +% current_row :当前列数 +% current_col :当前行数 +% +% 注意:使用如下语句,print保存图片的时候使其按照设置来保存,否则修改无效 +% set(gcf, 'PaperPositionMode', 'auto'); + +% author : songxf +% time : 2020-03-06 +% email : + +function [] = RemoveSubplotWhiteArea(gca, sub_row, sub_col, current_row, current_col) +% 设置OuterPosition +sub_axes_x = current_col*1/sub_col - 1/sub_col; +sub_axes_y = 1-current_row*1/sub_row; % y是从上往下 +sub_axes_w = 1/sub_col; +sub_axes_h = 1/sub_row; +set(gca, 'OuterPosition', [sub_axes_x, sub_axes_y, sub_axes_w, sub_axes_h]); % 重设OuterPosition + +% TightInset的位置 +inset_vectior = get(gca, 'TightInset'); +inset_x = inset_vectior(1); +inset_y = inset_vectior(2); +inset_w = inset_vectior(3); +inset_h = inset_vectior(4); + +% OuterPosition的位置 +outer_vector = get(gca, 'OuterPosition'); +pos_new_x = outer_vector(1) + inset_x+0.01; % 将Position的原点移到到TightInset的原点 +pos_new_y = outer_vector(2) + inset_y+0.01; +pos_new_w = outer_vector(3) - inset_w - inset_x-0.02; % 重设Position的宽 +pos_new_h = outer_vector(4) - inset_h - inset_y-0.02; % 重设Position的高 + +% 重设Position +set(gca, 'Position', [pos_new_x, pos_new_y, pos_new_w, pos_new_h]); diff --git a/VAR/VARirplotight.m b/VAR/VARirplotight.m new file mode 100644 index 0000000..c9b79ca --- /dev/null +++ b/VAR/VARirplotight.m @@ -0,0 +1,115 @@ +function VARirplotight(IRF,VARopt,INF,SUP) +% ======================================================================= +% Plot the IRFs computed with VARir +% ======================================================================= +% VARirplot(IRF,VARopt,vnames,INF,SUP) +% ----------------------------------------------------------------------- +% INPUT +% - IRF(:,:,:) : matrix with periods, variable, shock +% - VARopt: options of the VAR (see VARopt from VARmodel) +% ----------------------------------------------------------------------- +% OPTIONAL INPUT +% - INF: lower error band +% - SUP: upper error band +% ======================================================================= +% Ambrogio Cesa Bianchi, March 2015 +% ambrogio.cesabianchi@gmail.com + + +%% Check inputs +%================================================ +if ~exist('VARopt','var') + error('You need to provide VAR options (VARopt from VARmodel)'); +end +% If there is VARopt get the vnames +vnames = VARopt.vnames; +% Check they are not empty +if isempty(vnames) + error('You need to add label for endogenous variables in VARopt'); +end + +%% Retrieve and initialize variables +%================================================ +filename = [VARopt.figname 'IRF_']; +quality = VARopt.quality; +savefigs = VARopt.savefigs; +suptitle = VARopt.suptitle; +pick = VARopt.pick; + +% Initialize IRF matrix +[nsteps, nvars, nshocks] = size(IRF); + +% If one shock is chosen, set the right value for nshocks +if pick<0 || pick>nvars + error('The selected shock is non valid') +else + if pick==0 + pick=1; + else + nshocks = pick; + end +end + +% Define the rows and columns for the subplots +row = round(sqrt(nvars)); % 1; +col = ceil(sqrt(nvars)); % nvars; + +% Define a timeline +steps = 1:1:nsteps; +x_axis = zeros(1,nsteps); + + +%% Plot +%================================================ + +for jj=pick:nshocks + disp(['Impulse response to ' vnames{jj}]) + figure; + %FigSize(40,20) + %FigSize(40,25) + FigSize(40,32) + for ii=1:nvars + subplot(row,col,ii); + if exist('INF','var') && exist('SUP','var') + %shadedplot(steps,INF(:,ii,jj)',SUP(:,ii,jj)',[0.7 0.7 0.7],[0.7 0.7 0.7] ); %阴影部分需要先画,不然会覆盖后面画的曲线 + shadedplot(steps,INF(:,ii,jj)',SUP(:,ii,jj)',[0.8 0.8 0.95],[0.75 0.75 0.95] ); %阴影部分需要先画,不然会覆盖后面画的曲线 + hold on + plot(steps,INF(:,ii,jj),'LineStyle',':','Color',[0.39 0.58 0.93],'LineWidth',1.5); + hold on + plot(steps,SUP(:,ii,jj),'LineStyle',':','Color',[0.39 0.58 0.93],'LineWidth',1.5); + hold on + end + plot(steps,IRF(:,ii,jj),'LineStyle','-','Color',[0.01 0.09 0.44],'LineWidth',2); + hold on + plot(x_axis,'k','LineWidth',0.5); + xlim([1 nsteps]); + title([vnames{ii} ' to ' vnames{jj}], 'FontWeight','bold','FontSize',14); + grid on; + % new line to make tight plot + if mod(ii,col)==0 + RemoveSubplotWhiteArea(gca, row, col, ceil(ii/col), mod(ii,col)+col); + else + RemoveSubplotWhiteArea(gca, row, col, ceil(ii/col), mod(ii,col)); % 去除空白部分 + end + %if ii==nvars + % legend(shade,'68%置信区间') + %end + end + + if savefigs + % Save + FigName = [filename num2str(jj)]; + if quality + if suptitle==1 + Alphabet = char('a'+(1:nshocks)-1); + SupTitle([Alphabet(jj) ') IRF to a shock to ' vnames{jj}]) + end + set(gcf, 'Color', 'w'); + export_fig(FigName,'-pdf','-png','-painters') + else + print('-dpng','-r100',FigName); + orient landscape + print('-dpdf','-bestfit','-r100',FigName); + end + end +end