Chapter 5 Tables and Graphs
5.1 Matplotlib Base Plots
5.1.1 Line and Scatter Plots
Go back to fan’s Python Code Examples Repository (bookdown site) or the pyfan Package (API).
5.1.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
123)
np.random.seed(= np.random.normal(0, 2, 100)
ar_fl_y1_rand = np.cumsum(np.random.normal(0, 1, 100))
ar_fl_y2_rand = np.arange(1,len(ar_fl_y1_rand)+1)
ar_it_x_grid
# Start Figure
= plt.subplots()
fig, ax
# Graph
ax.plot(ar_it_x_grid, ar_fl_y1_rand,='blue', linestyle='dashed',
color='sd=2, 0 persistence') label
## [<matplotlib.lines.Line2D object at 0x000001716B9A56A0>]
ax.plot(ar_it_x_grid, ar_fl_y2_rand,='red', linestyle='solid',
color='sd=1, random walk')
label
# Labeling
## [<matplotlib.lines.Line2D object at 0x000001716E16BA30>]
='upper left') ax.legend(loc
## <matplotlib.legend.Legend object at 0x000001716E171D90>
'Random Standard Normal Draws') plt.ylabel(
## Text(0, 0.5, 'Random Standard Normal Draws')
'Time Periods') plt.xlabel(
## Text(0.5, 0, 'Time Periods')
'White Noise') plt.title(
## Text(0.5, 1.0, 'White Noise')
plt.grid() plt.show()
5.1.2 Text Plot
Go back to fan’s Python Code Examples Repository (bookdown site) or the pyfan Package (API).
5.1.2.1 Plot Text
Plot Text as Image. Create text with different alignment and rotation.
# Import Packages
import matplotlib.pyplot as plt
import textwrap
import json
# Dict of String to String
= {'C:\\Users\\fan\\Documents\\Dropbox (UH-ECON)\\repos\\Tex4Econ\\'
dc_path '_other\\equation\\cases.tex':
'C:/Users/fan/Documents/cases.pdf',
'C:\\Users\\fan\\Documents\\Dropbox (UH-ECON)\\repos\\Tex4Econ\\'
'_other\\symbols\\fs_symbols.tex':
'C:/Users/fan/Documents/fs_symbols.pdf'}
= textwrap.fill(json.dumps(dc_path), width = 70)
st_dc_path
# Start Plot
= plt.subplots()
fig, ax
# Text Plot
0.5, 0.5, st_dc_path,
ax.text(='center',
horizontalalignment='center',
verticalalignment=14, color='black',
fontsize=ax.transAxes)
transform
# Labeling
## Text(0.5, 0.5, '{"C:\\\\Users\\\\fan\\\\Documents\\\\Dropbox (UH-\nECON)\\\\repos\\\\Tex4Econ\\\\_other\\\\equation\\\\cases.tex":\n"C:/Users/fan/Documents/cases.pdf",\n"C:\\\\Users\\\\fan\\\\Documents\\\\Dropbox (UH-\nECON)\\\\repos\\\\Tex4Econ\\\\_other\\\\symbols\\\\fs_symbols.tex":\n"C:/Users/fan/Documents/fs_symbols.pdf"}')
ax.set_axis_off() plt.show()