我想使用axvspan()函数来可视化我用Pandas DataReader获得的DataFrame。但是当我使用下面的代码时,我看到一个错误,子图中没有阴影。我该怎么办?谢谢。
import matplotlib.pyplot as plt
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(['WFRBSB50215', 'WFRBST01134','WFRBST01122', 'WFRBSN09139', 'WFRBSB50189', 'WFRBST01110','WFRBSB50191'],'fred',start, end)
df.columns = ['Share of Total Net Worth Held by the Bottom 50% (1st to 50th Wealth Percentiles)',
'Share of Total Net Worth Held by the Top 1% (99th to 100th Wealth Percentiles)',
'Share of Corporate Equities and Mutual Fund Shares Helb By Top1%(99th to 100th Wealth Percentiles)',
'Share of Financial Assets Held by the 90th to 99th Wealth Percentiles',
'Share of Total Assets Held by the Bottom 50% (1st to 50th Wealth Percentiles)',
'Share of Real Estate Held by the Top 1% (99th to 100th Wealth Percentiles)',
'Share of Real Estate Held by the Bottom %50(1st to 50th Wealth Percentiles)'
]
ax = df.plot(subplots=True, layout=(7,1), figsize=(15,15), linewidth=3.5, colormap="summer")
ax.axvspan('2007-1-12', '2009-6-1', color='c', alpha=0.5)
ax.axvspan('2019-12-1', '2020-2-1',color= 'orange', alpha=0.5)
plt.xlabel('Date')
ax[0,].set_title('Share of Total Net Worth Held by the Bottom 50%')
ax[0,].set_ylabel('Percent of Aggregate')
ax[1,].set_title('Share of Total Net Worth Held by the Top 1%')
ax[1,].set_ylabel('Percent of Aggregate')
ax[2,].set_title('Share of Corporate Equities and Mutual Fund Shares Helb By Top 1%')
ax[2,].set_ylabel('Percent of Aggregate')
ax[3,].set_title('Share of Financial Assets Held by the 90th to 99th Wealth Percentiles')
ax[3,].set_ylabel('Percent of Aggregate')
ax[4,].set_title('Share of Total Assets Held by the Bottom 50% ')
ax[4,].set_ylabel('Percent of Aggregate')
ax[5,].set_title('Share of Real Estate Held by the Top 1%')
ax[5,].set_ylabel('Percent of Aggregate')
ax[6,].set_title('Share of Real Estate Held by the Bottom %50')
ax[6,].set_ylabel('Percent of Aggregate')
plt.tight_layout()
plt.style.use('seaborn-white')
plt.show()
1条答案
按热度按时间yhxst69z1#
尝试在所有子区上循环,并将
axvspan
添加到特定的AxesSubplot
:使用
plot
的ylabel
和xlabel
kwargs,并从df.columns
设置子情节标题,而不是手动设置。为
axvspan
添加图例。最简单的方法是为每个axvspan
添加一个标签,并在每次迭代结束时制作图例:或者,可以只为危机制作一个单独的图例:
^单个图例位于左下角。