[Python] Ravel () is convenient when drawing multiple graphs

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")   

FRED.png

Recommended Posts

[Python] Ravel () is convenient when drawing multiple graphs
[Python] Customize Colormap when drawing graphs with matplotlib
When specifying multiple keys in python sort
python note: when easy_install is not available
[Python] What is inherited by multiple inheritance?
Python memo using perl --format is convenient.
[Drawing and labeling multiple graphs] Plotly dynamic visualization [python3, make subplot, xlabel, ylabel]
[Python] How to draw multiple graphs with Matplotlib
Multiple graphs are displayed in one window (python)
[python] Streamline drawing
Python is easy
What is python
Python is instance
What is Python
When there are multiple Selenium SeleniumBasic classes (Python VBA)
Divisor enumeration when prime factorization is already known (Python)