# Matlab plotting function

**URL:** <https://forum.dynare.org/t/matlab-plotting-function/15363>\
**Category:** General DSGE Modeling\
**Created:** [12 March 2020 15:20 UTC](https://forum.dynare.org/t/matlab-plotting-function/15363 "2020-03-12T15:20:13Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Julie](https://forum.dynare.org/letter_avatar_proxy/v4/letter/j/4af34b/32.png) [@Julie](https://forum.dynare.org/u/Julie)\
**Post date:** [12 March 2020 15:20 UTC](https://forum.dynare.org/t/matlab-plotting-function/15363/1 "2020-03-12T15:20:13Z")

</div>

Hi Professor

I am have a bit of trouble plotting data with matlab …  
The data is quartely, and starts from 1986Q4 to 2016Q4,  
and I want to specify time axis with Matlab…

clear all; close all; clc;  
load a1.csv;

k=0;

for i=1986:2016;  
if i==1986  
quarters=[12];  
else  
quarters=[3 6 9 12];  
end

```
    for j=quarters
        k=k+1;
        T(k,1)=datenum(i,j,1);
    end

```

end

plot(T,a1,‘b’,‘LineWidth’,2);  
set(gca,‘XTick’,[T(1):365\*1:T(end)]);  
datetick(‘x’,‘yy’,‘keepticks’);  
recessionplot;  
ax = gca;  
ax.YAxis.Exponent=0;  
set(gca,‘FontSize’,14, ‘FontName’,‘Times’);

However, when specifying dates as shown below in “test.m”, the plot I got isn’t exactly correct.

 ![image](https://forum.dynare.org/uploads/default/original/2X/0/09703403b0325f74776bd1d6b541b52a94e98c81.png)

From the plot below, it looks like the data starts from somewhere in 86 and ends in 15Q4….  
Please could you indicate me the problem??

Q2. the “recessionplot” build-in function with Matlab is based on the US data. However, if I want to customize it for the EU data or UK data, how am I suppossed to mofify the “recessionplot” part?

say, if I want to specify the recession for the UK as 2008Q1-2009Q1, how am I supposed to modify my clode?

[a1.csv](https://forum.dynare.org/uploads/short-url/9t1ly46uFdvTF69LcfdEE2Az6vu.csv) (1.1 KB) [test.m](https://forum.dynare.org/uploads/short-url/tZax29Hgzt6pd4YCiwIZBGsiTgN.m) (456 Bytes)

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [15 March 2020 08:10 UTC](https://forum.dynare.org/t/matlab-plotting-function/15363/2 "2020-03-15T08:10:54Z")

</div>

1. This is due to Matlab changing the xlim. You need to preserve the limits. The following works:

```auto
clear all; close all; clc;
load a1.csv;
T=size(a1,1);

dates=datenum([1986;kron([1987:2016]',ones(4,1))],[10;repmat([1:3:10]',30,1)],ones(T,1));

figure;
plot(dates,a1,'b','LineWidth',2);
hh=gca;
axis tight
set(gca,'Xtick',dates(2:4:end-4))
set(gca,'XtickLabel',dates(2:4:end-4))
datetick('x','yy','keepticks','keeplimits');
recessionplot;
ax = gca;
ax.YAxis.Exponent=0; 
set(gca,'FontSize',14, 'FontName','Times');`

```

1. The dates of recessions can be passed using the `'recessions' — Recession data` name/value pair of the function. See the Matlab help.

---

<div class="post-metadata">

**Author:** ![Julie](https://forum.dynare.org/letter_avatar_proxy/v4/letter/j/4af34b/32.png) [@Julie](https://forum.dynare.org/u/Julie)\
**Post date:** [15 March 2020 22:19 UTC](https://forum.dynare.org/t/matlab-plotting-function/15363/3 "2020-03-15T22:19:13Z")

</div>

Professor,  
Q1, the code gave me:

 ![image](https://forum.dynare.org/uploads/default/original/2X/4/4d2c6c407c27454b63b74ab132119b2a9be46dc1.png)  
with the datatick of last year 2016 missing (data spans 1986Q4-2016Q4)

when I tried to modify “set(gca,‘XtickLabel’,dates(2:4:end-4))” to “set(gca,‘XtickLabel’,dates(2:4:end))”, it gave me:

 ![image](https://forum.dynare.org/uploads/default/original/2X/c/ca55238ad4dca971829194a431460ba9c3a9a034.png)

the duration of the year 2016 was shorter than those of previous years, like 2015…

so I think there might be some problems…

besides, if I want to plot the time interval as 2 years, how am I supposed to modify the code?  
I tried to modify “set(gca,‘Xtick’,dates(2:4:end-4))  
set(gca,‘XtickLabel’,dates(2:4:end-4))” to "set(gca,‘Xtick’,dates(2:8:end-4))  
set(gca,‘XtickLabel’,dates(2:8:end-4)), but only got:

 ![image](https://forum.dynare.org/uploads/default/original/2X/8/88af1cabbd2b62faa59057ded03bcd68d635c10d.png)

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [16 March 2020 07:46 UTC](https://forum.dynare.org/t/matlab-plotting-function/15363/4 "2020-03-16T07:46:56Z")

</div>

Of course 2016 it is shorter, because the data ends before 2017. That is also the reason it is not shown.

---

<div class="post-metadata">

**Author:** ![Julie](https://forum.dynare.org/letter_avatar_proxy/v4/letter/j/4af34b/32.png) [@Julie](https://forum.dynare.org/u/Julie)\
**Post date:** [25 March 2020 20:48 UTC](https://forum.dynare.org/t/matlab-plotting-function/15363/5 "2020-03-25T20:48:47Z")

</div>

hi professor,

In Small-Open-Economy framework where domestic households have access to both domestic (B) and foreign bonds ( B\_\*).

would the debt/gdp ratio be defined as B/Y?

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [26 March 2020 08:53 UTC](https://forum.dynare.org/t/matlab-plotting-function/15363/6 "2020-03-26T08:53:53Z")

</div>

That question is not well-defined. Which debt/GDP ratio are you referring to? Net or gross? Private or public? External or total?

---

<div class="post-metadata">

**Author:** ![Julie](https://forum.dynare.org/letter_avatar_proxy/v4/letter/j/4af34b/32.png) [@Julie](https://forum.dynare.org/u/Julie)\
**Post date:** [1 April 2020 22:03 UTC](https://forum.dynare.org/t/matlab-plotting-function/15363/8 "2020-04-01T22:03:08Z")

</div>

I was referring to public debt, assuming that the households have access to both domestic and foreign bonds.  
Thanks

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [2 April 2020 16:25 UTC](https://forum.dynare.org/t/matlab-plotting-function/15363/9 "2020-04-02T16:25:25Z")

</div>

If you consider total public debt and the government issues that debt domestically and internationally, you want to have internal and external debt. So you need to add B and B^\* (and potentially transform them to the same units)

---

<div class="post-metadata">

**Author:** ![Julie](https://forum.dynare.org/letter_avatar_proxy/v4/letter/j/4af34b/32.png) [@Julie](https://forum.dynare.org/u/Julie)\
**Post date:** [8 April 2020 10:50 UTC](https://forum.dynare.org/t/matlab-plotting-function/15363/10 "2020-04-08T10:50:04Z")

</div>

Hi Professor,

further to my previous question,  
If I want to plot every 3 years on x-axis (data label), e.g. 1987,1990,1993,1996…how am I supposed to modify the codes below:

clear all; close all; clc;  
load a1.csv;  
T=size(a1,1);

dates=datenum([1986;kron([1987:2016]’,ones(4,1))],[10;repmat([1:3:10]’,30,1)],ones(T,1));

figure;  
plot(dates,a1,‘b’,‘LineWidth’,2);  
hh=gca;  
axis tight  
set(gca,‘Xtick’,dates(2:4:end-4))  
set(gca,‘XtickLabel’,dates(2:4:end-4))  
datetick(‘x’,‘yy’,‘keepticks’,‘keeplimits’);  
recessionplot;  
ax = gca;  
ax.YAxis.Exponent=0;  
set(gca,‘FontSize’,14, ‘FontName’,‘Times’);

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [10 April 2020 07:10 UTC](https://forum.dynare.org/t/matlab-plotting-function/15363/11 "2020-04-10T07:10:43Z")

</div>

Then you need to use steps of 12 quarters:

```auto
set(gca,'Xtick',dates(2:12:end))
set(gca,'XtickLabel',dates(2:12:end))

```

---

<div class="post-metadata">

**Author:** ![Julie](https://forum.dynare.org/letter_avatar_proxy/v4/letter/j/4af34b/32.png) [@Julie](https://forum.dynare.org/u/Julie)\
**Post date:** [10 April 2020 22:40 UTC](https://forum.dynare.org/t/matlab-plotting-function/15363/13 "2020-04-10T22:40:57Z")

</div>

Please could you also indicate me :

Q1. if the data spans 1986Q4 to 2016Q3, then how should I mofidy the part below?

dates=datenum([1986;kron([1987:2016]’,ones(4,1))],[10;repmat([1:3:10]’,30,1)],ones(T,1));  
set(gca,‘Xtick’,dates(2:4:end-4))  
set(gca,‘XtickLabel’,dates(2:4:end-4))

Q2. what if the data spans: 1986Q1 to 2016Q4？

Q3. What if the data are monthly?

Thanks!

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [13 April 2020 09:50 UTC](https://forum.dynare.org/t/matlab-plotting-function/15363/14 "2020-04-13T09:50:07Z")

</div>

The part in the brackets

```auto
dates=datenum([1986;kron([1987:2016]’,ones(4,1))],[10;repmat([1:3:10]’,30,1)],ones(T,1));

```

creates a date number of the format YYYY,MM,DD. The first part is about the year. Here, we start with 1986. In the month part, we start with month 10, i.e. quarter 4. The `kron([1987:2016]’,ones(4,1))` part repeats the years 1987 to 2016 four times as we are matching it to the 4 months indicating the respective quarters. So if you want 1986Q4 to 2016Q3:

```auto
T=length(1986.75:0.25:2016.5)'
dates=datenum([1986;kron([1987:2015]',ones(4,1)); 2016; 2016; 2016],[10;repmat([1:3:10]',29,1); 1;4;7],ones(T,1));

```

while it is for 1986Q4 to 2016Q4

```auto
T=length(1986:0.25:2016.75)'
dates=datenum([kron([1986:2016]',ones(4,1))],[repmat([1:3:10]',31,1)],ones(T,1));

```

Monthly is similar

```auto
T=length(1986:1/12:2016+11/12)'
dates=datenum([kron([1986:2016]',ones(12,1))],[repmat([1:12]',31,1)],ones(T,1));

```

---

<div class="post-metadata">

**Author:** ![Julie](https://forum.dynare.org/letter_avatar_proxy/v4/letter/j/4af34b/32.png) [@Julie](https://forum.dynare.org/u/Julie)\
**Post date:** [13 April 2020 14:49 UTC](https://forum.dynare.org/t/matlab-plotting-function/15363/16 "2020-04-13T14:49:10Z")

</div>

is there a typo in your reply? as it should be “while it is for 1986Q1 to 2016Q4”, right?

Please could you explain a bit why did you change [10;repmat([1:3:10]’,30,1)] to [10;repmat([1:3:10]’,29,1)] and [10;repmat([1:3:10]’,31,1)] ?

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [13 April 2020 15:26 UTC](https://forum.dynare.org/t/matlab-plotting-function/15363/17 "2020-04-13T15:26:31Z")

</div>

The number 29 or 30 denotes the number of full years in the middle where all three quarters are present.
