Reputation: 31
I'm trying to plot one diagram of both cyclical components(gdp and hoursworked) using HP-filters and matplotlib in python. Currently only getting one figure to plot and other figure is flatline(HoursWorked is flatlined) (Image below). Any pointers on how to improve code.
import statsmodels.api as sm
import matplotlib.pyplot as plt
pandas_datareader import data
import datetime as dt
start, end = dt.datetime(1965, 1, 1), dt.datetime(2016,12, 31)
gdp = data.DataReader('GDPC1', 'fred', start, end)
HoursWorked = data.DataReader('PRSCQ', 'fred', start, end)
plt.figure(1)
plt.subplot(211)
plt.plot(gdp)
plt.title('RealGDP and Hours Worked')
cycle, trend = sm.tsa.filters.hpfilter(gdp, 1600)
plt.figure(1)
plt.subplot(211)
plt.plot(HoursWorked)
ax = plt.gca()
ax.set_xticklabels([])
plt.show()
[
2
Upvotes: 1
Views: 14455
Reputation: 339705
I think you would want to use a twin axes. I.e. you want two axes which sit on top ofeach other and share the same x scale, but have a different y scale. This can be done with the following code:
import matplotlib.pyplot as plt
import pandas_datareader.data
import datetime as dt
start, end = dt.datetime(1965, 1, 1), dt.datetime(2016,12, 31)
gdp = pandas_datareader.data.DataReader('GDPC1', 'fred', start, end)
HoursWorked = pandas_datareader.data.DataReader('PRSCQ', 'fred', start, end)
fig, ax = plt.subplots()
ax2 = ax.twinx()
ax.plot(gdp, label="GDP")
ax2.plot(HoursWorked, color="C2", label="HoursWorked")
ax.set_title('RealGDP and Hours Worked')
ax.legend(loc=2)
ax2.legend(loc=4)
plt.show()
Upvotes: 2