seaborn axvspan()两行之间的着色

68bkxrlz  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(228)

我不能使用seaborn和pandas数据框来为两个日期之间的区域着色。代码如下。

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 (2020,12,31)
df = pdr.DataReader(['FEDFUNDS','TERMCBPER24NS','TERMCBCCALLNS','T5YIFR','T10YIE'], 'fred', start, end)
df.columns = ['Effective Federal Funds Rate','Finance Rate on Personal Loans at Commercial Banks','Commercial Bank Interest Rate on Credit Card Plans',
             '5-Year, 5-Year Forward Inflation Expectation Rate','10-Year Breakeven Inflation Rate']

fig, axes = plt.subplots(5,1, figsize=(20,25))
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].set_title('Effective Federal Funds Rate')
sns.scatterplot(x='DATE', y='Effective Federal Funds Rate',  data=df,  s=50, ax=axes[0])
axes[1].set_title('Finance Rate on Personal Loans at Commercial Banks')
sns.scatterplot(x='DATE', y='Finance Rate on Personal Loans at Commercial Banks',  data=df, s=50,ax=axes[1])
axes[2].set_title('Commercial Bank Interest Rate on Credit Card Plan')
sns.scatterplot(x='DATE', y='Commercial Bank Interest Rate on Credit Card Plans',  data=df,  s=50, ax=axes[2]) 
axes[3].set_title('5-Year, 5-Year Forward Inflation Expectation Rate')
sns.scatterplot(x='DATE', y='5-Year, 5-Year Forward Inflation Expectation Rate', data=df,  s=50, ax=axes[3])
axes[4].set_title('10-Year Breakeven Inflation Rate')
sns.scatterplot(x='DATE', y='10-Year Breakeven Inflation Rate',  data=df,  s=50, ax=axes[4])
plt.tight_layout
plt.style.use('seaborn-whitegrid')

当我从上面的代码中删除下面的函数时,

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)

一切正常。但是我不能用上面的代码来遮住两个区域。我该怎么做来遮挡这些区域?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题