Matplotlib axvspan() 函数填写错误的日期

Matplotlib axvspan() function fills wrong date

当我想用axvspan填充日期时,它填充了错误的区域。我认为它逆转了 axvspans。问题的原因可能是我从世界银行导入的数据框。我该如何解决?谢谢。

import matplotlib.pyplot as plt
import matplotlib
from pandas_datareader import wb
import seaborn as sns
import pandas as pd
import datetime
start = datetime.datetime (2000,1,1)
end = datetime.datetime (2021,5,1)
ind = ['FM.LBL.BMNY.ZG',
       'FR.INR.DPST'
       ]
df = wb.download(indicator=ind, country='CHN', start=start, end=end).dropna();df.reset_index(inplace=True)
df.columns = ['Country',
              'Year',
              'Broad money growth (annual %) - China',
              'Deposit interest rate (%)'
              ]
df=df.sort_values(by='Year', ascending=True)
axes= df.plot(x='Year',subplots=True, figsize=(20,12), layout=(1,2), colormap='summer', legend=True)

for ax, col in zip(axes.flatten(), 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,0].set_title('Broad money growth (annual %) - China')
axes[0,0].invert_xaxis()
axes[0,0].legend(loc='upper left')
axes[0,0].set_ylabel('Percent(Annual)')
axes[0,0].invert_xaxis()

axes[0,1].set_title('Deposit interest rate (%)')
axes[0,1].invert_xaxis()
axes[0,1].legend(loc='upper left')
axes[0,1].set_ylabel('Percent(Annual)')
axes[0,1].invert_xaxis()



plt.suptitle("Financial Sector in China",fontweight="bold")
plt.show()

正在更正 pandas.plot 中的 xaxis dtype 以更正 vspan 位置

df 中的年份列是一个对象而不是 int,因此 xlim 是 (-1, 21) 并且标签在这些索引位置之上进行格式化。因此,当你去放置你的 vspan 时,输入的 x 位置与 x 轴的位置不匹配。要解决此问题,只需将您的年份列设为 int 并将年份的 vspan 设为整数。

import matplotlib.pyplot as plt
import matplotlib
from pandas_datareader import wb
import seaborn as sns
import pandas as pd
import datetime
import matplotlib.ticker as ticker

start = datetime.datetime (2000,1,1)
end = datetime.datetime (2021,5,1)
ind = ['FM.LBL.BMNY.ZG',
       'FR.INR.DPST'
       ]
df = wb.download(indicator=ind, country='CHN', start=start, end=end).dropna();df.reset_index(inplace=True)
df.columns = ['Country',
              'Year',
              'Broad money growth (annual %) - China',
              'Deposit interest rate (%)'
              ]
df=df.sort_values(by='Year', ascending=True)
df['Year'] = df['Year'].astype(int)

axes= df.plot(x='Year',subplots=True, figsize=(15,5), layout=(1,2), colormap='summer', legend=True)

for ax, col in zip(axes.flatten(), df.columns):
    ax.axvspan(2007, 2009, color='teal', alpha=0.5, label='2008 Crisis')
    ax.axvspan(2019, 2020, color='orange', alpha=0.5, label='Pandemic')
    ax.set_title(col)
    ax.set_xlim(2000, 2021)
    ax.xaxis.set_major_locator(ticker.MultipleLocator(2))

axes[0,0].set_title('Broad money growth (annual %) - China')
axes[0,0].legend(loc='upper left')
axes[0,0].set_ylabel('Percent(Annual)')


axes[0,1].set_title('Deposit interest rate (%)')
axes[0,1].legend(loc='upper left')
axes[0,1].set_ylabel('Percent(Annual)')

plt.suptitle("Financial Sector in China",fontweight="bold")