1 Line and Scatter Plots

Go to the RMD, PDF, or HTML version of this file. Go back to Python Code Examples Repository (bookdown site) or the pyfan Package (API).

1.1 Plot Random Walk and White Noise Jointly

Given x and y coordinates, plot out two lines. see matplotlib.pyplot.plot. Here we will plot out the extremes of AR(1), white noise (no persistence), and random walk (fully persistent shocks).

# Import Packages
import numpy as np
import matplotlib.pyplot as plt

# Generate X and Y
np.random.seed(123)
ar_fl_y1_rand = np.random.normal(0, 2, 100)
ar_fl_y2_rand = np.cumsum(np.random.normal(0, 1, 100))
ar_it_x_grid = np.arange(1,len(ar_fl_y1_rand)+1)

# Start Figure
fig, ax = plt.subplots()

# Graph
ax.plot(ar_it_x_grid, ar_fl_y1_rand,
                     color='blue', linestyle='dashed',
                     label='sd=2, 0 persistence')
## [<matplotlib.lines.Line2D object at 0x0000026F593E2FA0>]
ax.plot(ar_it_x_grid, ar_fl_y2_rand,
                     color='red', linestyle='solid',
                     label='sd=1, random walk')
                     
# Labeling
## [<matplotlib.lines.Line2D object at 0x0000026F598CF5E0>]
ax.legend(loc='upper left')
## <matplotlib.legend.Legend object at 0x0000026F56E7D100>
plt.ylabel('Random Standard Normal Draws')
## Text(0, 0.5, 'Random Standard Normal Draws')
plt.xlabel('Time Periods')
## Text(0.5, 0, 'Time Periods')
plt.title('White Noise')
## Text(0.5, 1.0, 'White Noise')
plt.grid()
plt.show()