简体   繁体   English

Seaborn axvspan() 两条线之间的阴影

[英]Seaborn axvspan() Shading between two lines

I can't shade the area between two dates with using seaborn and pandas data frame.我无法使用 seaborn 和 pandas 数据框对两个日期之间的区域进行着色。 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')

I think the answer to your previous question has axvspan enabled because it is automatically converted to a time series in the pandas plot.我认为您上一个问题的答案启用了 axvspan,因为它会自动转换为 Pandas 图中的时间序列。 If this code consists only of matplotlib, then you will need to transform the time series data.如果此代码仅包含 matplotlib,则您将需要转换时间序列数据。 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')

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM