How to reshape the x-axis in this case?

1 Ansicht (letzte 30 Tage)
Armando MAROZZI
Armando MAROZZI am 29 Okt. 2020
Kommentiert: Star Strider am 29 Okt. 2020
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.

Akzeptierte Antwort

Star Strider
Star Strider am 29 Okt. 2020
Either:
axis tight
or:
xlim([min(t) max(t)])
should work.
  2 Kommentare
Armando MAROZZI
Armando MAROZZI am 29 Okt. 2020
thanks!
Star Strider
Star Strider am 29 Okt. 2020
As always, my pleasure!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by