Skip to content

Commit d7fd2d7

Browse files
committed
update graph function and set global dpi
1 parent 92c5146 commit d7fd2d7

File tree

1 file changed

+36
-43
lines changed

1 file changed

+36
-43
lines changed

lectures/cagan_ree.md

Lines changed: 36 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ As usual, we'll start by importing some Python modules.
236236
import numpy as np
237237
from collections import namedtuple
238238
import matplotlib.pyplot as plt
239+
plt.rcParams['figure.dpi'] = 200
239240
```
240241
241242
First, we store parameters in a `namedtuple`:
@@ -338,7 +339,7 @@ Now we use the following function to plot the result
338339
339340
```{code-cell} ipython3
340341
def plot_sequences(sequences, labels):
341-
fig, axs = plt.subplots(len(sequences), 1, figsize=[5, 12], dpi=200)
342+
fig, axs = plt.subplots(len(sequences), 1, figsize=[5, 12])
342343
for ax, seq, label in zip(axs, sequences, labels):
343344
ax.plot(range(len(seq)), seq, label=label)
344345
ax.set_ylabel(label)
@@ -524,10 +525,11 @@ p_seq_2_regime2 = np.concatenate([p_seq_2_path1[:T1+1],
524525
525526
```{code-cell} ipython3
526527
:tags: [hide-input]
528+
527529
T_seq = range(T+2)
528530
529531
# plot both regimes
530-
fig, ax = plt.subplots(5, 1, figsize=[5, 12], dpi=200)
532+
fig, ax = plt.subplots(5, 1, figsize=[5, 12])
531533
532534
# Configuration for each subplot
533535
plot_configs = [
@@ -543,20 +545,22 @@ plot_configs = [
543545
'ylabel': r'$p$'}
544546
]
545547
546-
# Loop through each subplot configuration
547-
for axi, config in zip(ax, plot_configs):
548-
for data in config['data']:
549-
if len(data) == 3: # Plot with label for legend
550-
axi.plot(data[0], data[1], label=data[2])
551-
else: # Plot without label
552-
axi.plot(data[0], data[1])
553-
axi.set_ylabel(config['ylabel'])
554-
axi.set_xlabel(r'$t$')
555-
if 'label' in config: # If there's a label, add a legend
556-
axi.legend()
557-
558-
plt.tight_layout()
559-
plt.show()
548+
def generate_plots(plot_configs, ax):
549+
550+
# Loop through each subplot configuration
551+
for axi, config in zip(ax, plot_configs):
552+
for data in config['data']:
553+
if len(data) == 3: # Plot with label for legend
554+
axi.plot(data[0], data[1], label=data[2])
555+
axi.legend()
556+
else: # Plot without label
557+
axi.plot(data[0], data[1])
558+
axi.set_ylabel(config['ylabel'])
559+
axi.set_xlabel(r'$t$')
560+
plt.tight_layout()
561+
plt.show()
562+
563+
generate_plots(plot_configs, ax)
560564
```
561565
562566
We invite you to compare these graphs with corresponding ones for the foreseen stabilization analyzed in experiment 1 above.
@@ -582,36 +586,25 @@ unanticipated, as in experiment 2.
582586
583587
```{code-cell} ipython3
584588
:tags: [hide-input]
585-
# compare foreseen vs unforeseen shock
586-
fig, ax = plt.subplots(5, figsize=[5, 12], dpi=200)
587-
588-
plot_data = [
589-
(T_seq[:-1], [μ_seq_2], r'$\mu$', ['']),
590-
(T_seq, [π_seq_2, π_seq_1], r'$\pi$', ['Unforeseen', 'Foreseen']),
591-
(T_seq, [m_seq_2_regime1 - p_seq_2_regime1, m_seq_1 - p_seq_1],
592-
r'$m - p$', ['Unforeseen', 'Foreseen']),
593-
(T_seq, [m_seq_2_regime1, m_seq_2_regime2, m_seq_1], r'$m$',
594-
['Unforeseen (Smooth $m_{T_1}$)',
595-
'Unforeseen ($m_{T_1}$ jumps)',
596-
'Foreseen shock']),
597-
(T_seq, [p_seq_2_regime1, p_seq_2_regime2, p_seq_1], r'$p$',
598-
['Unforseen (Smooth $m_{T_1}$)',
599-
'Unforseen ($m_{T_1}$ jumps)',
600-
'Foreseen shock'])
601-
]
602589
603-
for i, (times, sequences, ylabel, labels) in enumerate(plot_data):
604-
for seq, label in zip(sequences, labels):
605-
ax[i].plot(times, seq, label=label)
606-
ax[i].set_ylabel(ylabel)
607-
if labels[0]:
608-
ax[i].legend()
590+
# compare foreseen vs unforeseen shock
591+
fig, ax = plt.subplots(5, figsize=[5, 12])
609592
610-
for axis in ax:
611-
axis.set_xlabel(r'$t$')
593+
plot_configs = [
594+
{'data': [(T_seq[:-1], μ_seq_2)], 'ylabel': r'$\mu$'},
595+
{'data': [(T_seq, π_seq_2, 'Unforeseen'),
596+
(T_seq, π_seq_1, 'Foreseen')], 'ylabel': r'$p$'},
597+
{'data': [(T_seq, m_seq_2_regime1 - p_seq_2_regime1, 'Unforeseen'),
598+
(T_seq, m_seq_1 - p_seq_1, 'Foreseen')], 'ylabel': r'$m - p$'},
599+
{'data': [(T_seq, m_seq_2_regime1, 'Unforeseen (Smooth $m_{T_1}$)'),
600+
(T_seq, m_seq_2_regime2, 'Unforeseen ($m_{T_1}$ jumps)'),
601+
(T_seq, m_seq_1, 'Foreseen')], 'ylabel': r'$m$'},
602+
{'data': [(T_seq, p_seq_2_regime1, 'Unforeseen (Smooth $m_{T_1}$)'),
603+
(T_seq, p_seq_2_regime2, 'Unforeseen ($m_{T_1}$ jumps)'),
604+
(T_seq, p_seq_1, 'Foreseen')], 'ylabel': r'$p$'}
605+
]
612606
613-
plt.tight_layout()
614-
plt.show()
607+
generate_plots(plot_configs, ax)
615608
```
616609
617610
It is instructive to compare the preceding graphs with graphs of log price levels and inflation rates for data from four big inflations described in

0 commit comments

Comments
 (0)