Search code examples
pythonpandasmatplotlibseabornpandas-datareader

Seaborn axvspan() Shading between two lines


I can't shade the area between two dates with using seaborn and pandas data frame. Codes are as follows. How can I do that. I can't loop the function. I don't know how to loop it.

import matplotlib.pyplot as plt 
import seaborn as sns 
import pandas_datareader.data as pdr 
import pandas as pd 
import datetime
import matplotlib.dates as mdates

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(mdates.date2num(datetime.datetime(2007,1,12)), mdates.date2num(datetime.datetime(2009,6,1)), color='teal', alpha=0.5,
           label='2008 Crisis')
ax.axvspan(mdates.date2num(datetime.datetime(2019,12,1)), mdates.date2num(datetime.datetime(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')

Solution

  • I think the answer to your previous question has axvspan enabled because it is automatically converted to a time series in the pandas plot. If this code consists only of matplotlib, then you will need to transform the time series data. See this.

    import matplotlib.pyplot as plt 
    import seaborn as sns 
    import pandas_datareader.data as pdr 
    import pandas as pd 
    import datetime
    import matplotlib.dates as mdates
    
    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(mdates.date2num(datetime.datetime(2007,1,12)), mdates.date2num(datetime.datetime(2009,6,1)), color='teal', alpha=0.5,
                   label='2008 Crisis')
        ax.axvspan(mdates.date2num(datetime.datetime(2019,12,1)), mdates.date2num(datetime.datetime(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')
    

    enter image description here