When drawing multiple graphs, using raise () makes it easier to control.
Since plt.subplots ()
creates a two-dimensional array in ʻaxes, you have to nest when drawing a graph with
for loop, but if you make it a one-dimensional array with
ravel ()`, nesting is unnecessary. ..
python
import pandas_datareader.data as web
import datetime
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
dic = {'Unemployment rate':'UNRATE',
'USHY spread': 'BAMLH0A0HYM2',
'CPI':'CPIAUCSL',
'10 year treasury': 'DGS10',
'Federal Funds Rate': 'FEDFUNDS',
'Industrial Production Index':'INDPRO',
'Household Debt to GDP': 'HDTGPDUSQ163N',
'Real GDP': 'A191RL1Q225SBEA',
'WTI oil price': 'WTISPLC',
'VIX': 'VIXCLS',
'S&P500': 'SP500'}
source = 'fred'
start = datetime.datetime(2015, 1, 1)
end = datetime.datetime(2020, 5, 1)
h = ((len(dic)+1)//2)*4
fig, axes = plt.subplots(6,2, figsize = (15,h))
for ax, (ttl, symbol) in zip(axes.ravel(),dic.items()):
dt = web.DataReader(symbol,source, start, end)
ax.plot(dt)
ax.set_title(ttl)
ax.grid(True)
plt.tight_layout()
plt.savefig('FRED',bbox_inches="tight")
Recommended Posts