How to reshape the x-axis in this case?

조회 수: 1 (최근 30일)
Armando MAROZZI
Armando MAROZZI 2020년 10월 29일
댓글: Star Strider 2020년 10월 29일
I have a chart (attached) that starts the sample in 2008 despite the actual data starts in 2009-01. I would like to understand why and correct it.
This is an example that replicates the issue:
t1 = datetime(2009,01,01);
t2 = datetime(2019,11,01);
t = t1:calmonths:t2;
t= datenum(char(t),'dd-mm-yyyy');
subplot(3,1,1)
% 1st shaded area
d1=datenum(datetime(2009,01,01));
d2=datenum(datetime(2009,06,01));
x=[[d1,d2],[d2,d1],d1];
y=[-[6,6],[6,6],[-6]];
fill(x, y, [0.7 0.7 0.7],'LineStyle','none', 'FaceAlpha', 0.2);
hold on
% 2nd shaded area
d1=datenum(datetime(2011,09,01));
d2=datenum(datetime(2013,03,01));
x=[[d1,d2],[d2,d1],d1];
y=[-[6,6],[6,6],[-6]];
fill(x, y, [0.7 0.7 0.7],'LineStyle','none', 'FaceAlpha', 0.2);
hold on
plot(t, randn(131,1), 'LineWidth',1,'Color', [0 0 0]);
ax.XTick = t(1:end);
datetick('x','yyyy','keepticks');
yline(0,'-')
ylabel('%')
title('Business Cycle Shock', 'FontWeight', 'Normal')
subplot(3,1,2)
% 1st shaded area
d1=datenum(datetime(2009,01,01));
d2=datenum(datetime(2009,06,01));
x=[[d1,d2],[d2,d1],d1];
y=[-[6,6],[6,6],[-6]];
fill(x, y, [0.7 0.7 0.7],'LineStyle','none', 'FaceAlpha', 0.2);
hold on
% 2nd shaded area
d1=datenum(datetime(2011,09,01));
d2=datenum(datetime(2013,03,01));
x=[[d1,d2],[d2,d1],d1];
y=[-[6,6],[6,6],[-6]];
fill(x, y, [0.7 0.7 0.7],'LineStyle','none', 'FaceAlpha', 0.2);
hold on
plot(t, randn(131,1), 'LineWidth',1,'Color', [0 0 0]);
hold on
ax.XTick = t(1:end);
datetick('x','yyyy','keepticks');
yline(0,'-')
ylabel('%')
title('Monetary Policy Shock', 'FontWeight', 'Normal')
subplot(3,1,3)
% 1st shaded area
d1=datenum(datetime(2009,01,01));
d2=datenum(datetime(2009,06,01));
x=[[d1,d2],[d2,d1],d1];
y=[-[6,6],[6,6],[-6]];
fill(x, y, [0.7 0.7 0.7],'LineStyle','none', 'FaceAlpha', 0.2);
hold on
% 2nd shaded area
d1=datenum(datetime(2011,09,01));
d2=datenum(datetime(2013,03,01));
x=[[d1,d2],[d2,d1],d1];
y=[-[6,6],[6,6],[-6]];
fill(x, y, [0.7 0.7 0.7],'LineStyle','none', 'FaceAlpha', 0.2);
hold on
plot(t, randn(131,1), 'LineWidth',1,'Color', [0 0 0]);
hold on
ax.XTick = t(1:end);
datetick('x','yyyy','keepticks');
yline(0,'-')
ylabel('%')
title('DFPA Shock', 'FontWeight', 'Normal')
In other words, I would like the grey bar to start in 2009 when my sample starts and not before.
Can anyone help me?
Thanks.

채택된 답변

Star Strider
Star Strider 2020년 10월 29일
Either:
axis tight
or:
xlim([min(t) max(t)])
should work.
  댓글 수: 2
Armando MAROZZI
Armando MAROZZI 2020년 10월 29일
thanks!
Star Strider
Star Strider 2020년 10월 29일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by