Issue
I am trying to create subplots using a Pandas DataFrame but some of them are blank and not displayed correctly. I don't know where I made a mistake. Pandas Data Reader sometimes gives an error when receiving data from FRED, when I convert subplots to seaborn scatterplots in such errors, the problem is solved, but what I need is a line plot. What should I do in that case? Thank you.
import matplotlib.pyplot as plt
import seaborn as sns
import pandas_datareader.data as pdr
import pandas as pd
import datetime
start = datetime.datetime (2000,1,1)
end = datetime.datetime (2021,5,1)
df = pdr.DataReader(['FDHBFRBN',
'FDHBFIN',
'FYGFDPUN',
'FRDGBSAM',
'HBFRGDQ188S',
'MTSMFBP133FMS',
'GFDEGDQ188S',
'GFDEBTN',
'FYFSD',
'FYFSGDA188S'], 'fred', start, end)
df.columns = ['Federal Debt Held by Federal Reserve Banks',
'Federal Debt Held by Foreign and International Investors',
'Federal Debt Held by the Public ',
'Federal Reserve Bank Held Gold Bullion: On Display',
'Federal Debt Held by Federal Reserve Banks as Percent of Gross Domestic Product',
'Means of Financing: Borrowing from the Public',
'Federal Debt: Total Public Debt',
'Federal Debt: Total Public Debt',
'Federal Surplus or Deficit',
'Federal Surplus or Deficit [-] as Percent of Gross Domestic Product']
axes = df.plot(subplots=True, layout=(5,2), figsize=(15,25), linewidth=2, colormap="summer")
for (ax,), col in zip(axes, df.columns):
ax.axvspan('2007-1-12', '2009-6-1', color='teal', alpha=0.5,
label='2008 Crisis')
ax.axvspan('2019-12-1', '2020-2-1', color='orange', alpha=0.5,
label='Pandemic')
ax.set_title(col)
axes[0,0].set_title('Federal Debt Held by Federal Reserve Banks')
axes[0,0].set_ylabel('Billions of Dollars(Quarterly)')
axes[0,0].legend(bbox_to_anchor=(1.05, 1))
axes[0,1].set_title('Federal Debt Held by Foreign and International Investors')
axes[0,1].set_ylabel('Billions of Dollars(Quarterly)')
axes[0,1].legend(bbox_to_anchor=(1.05, 1))
axes[1,0].set_title('Federal Debt Held by the Public ')
axes[1,0].set_ylabel('Millions of Dollars(Quarterly)')
axes[1,0].legend(bbox_to_anchor=(1.05, 1))
axes[1,1].set_title('Federal Reserve Bank Held Gold Bullion: On Display ')
axes[1,1].set_ylabel('Fine Troy Ounces(Monthly)')
axes[1,1].legend(bbox_to_anchor=(1.05, 1))
axes[2,0].set_title('Federal Debt Held by Federal Reserve Banks as Percent of Gross Domestic Product')
axes[2,0].set_ylabel('Percent of GDP(Quarterly)')
axes[2,0].legend(bbox_to_anchor=(1.05, 1))
axes[2,1].set_title('Means of Financing: Borrowing from the Public')
axes[2,1].set_ylabel('Millions of Dollars(Monthly)')
axes[2,1].legend(bbox_to_anchor=(1.05, 1))
axes[3,0].set_title('Federal Debt: Total Public Debt as Percent of Gross Domestic Product ')
axes[3,0].set_ylabel('Percent of GDP(Quarterly)')
axes[3,0].legend(bbox_to_anchor=(1.05, 1))
axes[3,1].set_title('Federal Debt: Total Public Debt')
axes[3,1].set_ylabel('Millions of Dollars(Qaurterly)')
axes[3,1].legend(bbox_to_anchor=(1.05, 1))
axes[4,0].set_title('Federal Surplus or Deficit')
axes[4,0].set_ylabel('Billions of Dollars(Annual)')
axes[4,0].legend(bbox_to_anchor=(1.05, 1))
axes[4,1].set_title('Federal Surplus or Deficit [-] as Percent of Gross Domestic Product')
axes[4,1].set_ylabel('Percent of GDP(Annual)')
axes[4,1].legend(bbox_to_anchor=(1.05, 1))
ax.set_title("US Reserves")
plt.xlabel('Date')
plt.tight_layout()
plt.style.use('seaborn-whitegrid')
plt.rcParams.update({'font.family':'Latin Modern Sans'})
Solution
Your for
loop should be like this instead.
for ax, col in zip(axes.flatten(), df.columns):
...
You need to flatten the axes
grid, making in one dimensional, otherwise the zip
function won't work properly, hence the empty plots.
Answered By - Xelvoz
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.