微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

某些 Pandas 子图为空白或未正确显示

如何解决某些 Pandas 子图为空白或未正确显示

我正在尝试使用 Pandas DataFrame 创建子图,但其中一些为空白且无法正确显示。我不知道我哪里出错了。 Pandas Data Reader 在从 FRED 接收数据时有时会出错,当我在此类错误中将子图转换为 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 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',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,1].set_title('Federal Reserve Bank Held Gold Bullion: On display ')
axes[1,1].set_ylabel('Fine Troy Ounces(Monthly)')
axes[1,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,1].set_title('Means of Financing: Borrowing from the Public')
axes[2,1].set_ylabel('Millions of Dollars(Monthly)')
axes[2,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,1].set_title('Federal Debt: Total Public Debt')
axes[3,1].set_ylabel('Millions of Dollars(Qaurterly)')
axes[3,1))

axes[4,0].set_title('Federal Surplus or Deficit')
axes[4,0].set_ylabel('Billions of Dollars(Annual)')
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))


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 函数将无法正常工作,因此会出现空图。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。