【发布时间】:2020-05-08 16:55:30
【问题描述】:
我在这里遵循了我的问题的所有步骤:Pandas Dataframe : How to add a vertical line with label to a bar plot when your data is time-series?
它应该可以解决我的问题,但是当我将绘图类型更改为线时,垂直线没有出现。我复制相同的代码并将绘图类型更改为 line 而不是 bar:
你可以看到 bar ,垂直线(红色)出现。
# function to plot a bar
def dessine_line3(madataframe,debut_date , mes_colonnes):
madataframe.index = pd.to_datetime(madataframe.index,format='%m/%d/%y')
df = madataframe.loc[debut_date:,mes_colonnes].copy()
filt = (df[df.index == '4/20/20']).index
df.index.searchsorted(value=filt)
fig,ax = plt.subplots()
df.plot.bar(figsize=(17,8),grid=True,ax=ax)
ax.axvline(df.index.searchsorted(filt), color="red", linestyle="--", lw=2, label="lancement")
plt.tight_layout()
但是我只是通过将绘图类型更改为 line 来更改代码:没有垂直线,x 轴(日期)也发生了变化。
所以我写了另一个代码来用垂直线画线
ax = madagascar_maurice_case_df[["Madagascar Covid-19 Ratio","Maurice Covid-19 Ratio"]].loc['3/17/20':].plot.line(figsize=(17,7),grid=True)
filt = (df[df.index=='4/20/20']).index ax.axvline(df.index.searchsorted(filt),color="red",linestyle="--",lw=2 ,label="lancement") plt.show()
但结果是一样的
根据下面的评论,这是我的最终代码:
def dessine_line5(madataframe,debut_date , mes_colonnes):
plt.figure(figsize=(17,8))
plt.grid(b=True,which='major',axis='y')
df = madataframe.loc[debut_date:,mes_colonnes]
sns.lineplot(data=df)
lt = datetime.toordinal(pd.to_datetime('4/20/20'))
plt.axvline(lt,color="red",linestyle="--",lw=2,label="lancement")
plt.show()
【问题讨论】:
-
请提供完整的mcve。
标签: python pandas dataframe matplotlib