Pandas 部分子图为空白或显示不正确
Some of Pandas subplots is blank or not displayed correctly
我正在尝试使用 Pandas DataFrame 创建子图,但其中一些是空白的并且显示不正确。我不知道我在哪里犯了错误。 Pandas Data Reader 从FRED接收数据时有时会报错,当我在此类报错中将subplots转换为seaborn散点图时,问题解决,但我需要的是线图。在这种情况下我该怎么办?谢谢。
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'})
你的 for
循环应该是这样的。
for ax, col in zip(axes.flatten(), df.columns):
...
您需要将 axes
网格展平,使其成为一维,否则 zip
功能将无法正常工作,因此出现空白图。
我正在尝试使用 Pandas DataFrame 创建子图,但其中一些是空白的并且显示不正确。我不知道我在哪里犯了错误。 Pandas Data Reader 从FRED接收数据时有时会报错,当我在此类报错中将subplots转换为seaborn散点图时,问题解决,但我需要的是线图。在这种情况下我该怎么办?谢谢。
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'})
你的 for
循环应该是这样的。
for ax, col in zip(axes.flatten(), df.columns):
...
您需要将 axes
网格展平,使其成为一维,否则 zip
功能将无法正常工作,因此出现空白图。