小编Jul*_*one的帖子

如何向时间序列数据的 pandas 条形图添加垂直线

我使用 Pandas 并假设我们有以下 DataFrame :

ax = madagascar_case[["Ratio"]].loc['3/17/20':]
ax.tail()
Run Code Online (Sandbox Code Playgroud)

出去 : 在此输入图像描述

我想显示以下比率值的条形图,并添加与特定日期相关的垂直线,例如:“4/20/20”:

当我尝试下面的代码时:

ax = madagascar_case[["Ratio"]].loc['3/17/20':].plot.bar(figsize=(17,7), grid = True)
# to add a vertical line
ax.axvline("4/20/20",color="red",linestyle="--",lw=2 ,label="lancement")
Run Code Online (Sandbox Code Playgroud)

结果是垂直线(红色)的日期错误并且没有标签:

在此输入图像描述

因此,为了解决这个问题,我使用 matplotlib 尝试了另一个代码:

p = '4/20/20'
# Dataframe 
ax = madagascar_case[["Ratio"]].loc['3/17/20':]
# plot a histogram based on ax 
plt.hist(ax,label='ratio')
# add vertical line 
plt.axvline(p,color='g',label="lancement")

plt.legend()
plt.show()
Run Code Online (Sandbox Code Playgroud)

结果比预想的还要糟糕。:

在此输入图像描述

有没有最简单的方法来解决这个问题?

RVA92 >> 我遵循了你的最后一个代码:

df  = madagascar_case.loc['3/19/20':,'Ratio'].copy()
fig,ax = plt.subplots()
# plot bars 
df.plot.bar(figsize=(17,7),grid=True,ax=ax)
ax.axvline(df.index.searchsorted('4/9/20'), color="red", linestyle="--", lw=2, label="lancement")
plt.tight_layout()
Run Code Online (Sandbox Code Playgroud)

结果是,例如,当我将日期更改为“4/9/20”时,它可以工作,但是当我将日期更改为“4/20/20”时,它不正确,我不知道为什么?

ax.axvline(df.index.searchsorted('4/20/20'), color="red", linestyle="--", lw=2, …
Run Code Online (Sandbox Code Playgroud)

python matplotlib pandas

1
推荐指数
1
解决办法
3649
查看次数

Seaborn lineplot :注释最后一个值

我想使用 subplots 绘制 2 个数字。我有一个数据框(simple_line_final_df)

    Madagascar Covid-19 Totaly olona
Daty        
2020-05-20  Marary  371
2020-05-20  Sitrana 131
2020-05-20  Tsaboana    238
2020-05-21  Marary  405
2020-05-21  Sitrana 131
... ... ...
2020-06-28  Sitrana 944
2020-06-28  Tsaboana    1116
2020-06-29  Marary  2138
2020-06-29  Sitrana 966
2020-06-29  Tsaboana    1152
Run Code Online (Sandbox Code Playgroud)

我的目标是显示线图中的最后一个值(图 1):

使用以下代码:

fig ,axes = plt.subplots(ncols=1,nrows=2,constrained_layout=True)

# # ------------------------------Plot first figure --------------------------------------------
palette = ['#F70A0A','#2A930C','#930C85']
sns.set(rc={'figure.figsize':(30,15)},palette= palette, font_scale=1.7)
# # pour les axes 

ax1 = sns.lineplot(x=simple_line_final_df.index,y= 'Totaly olona',data=simple_line_final_df,hue='Madagascar Covid-19',style='Madagascar Covid-19',markers=True,dashes=False,ax=axes[0])

# style 
sns.set_style("darkgrid" , {"ytick.major.size": 10 …
Run Code Online (Sandbox Code Playgroud)

matplotlib seaborn

1
推荐指数
1
解决办法
2822
查看次数

标签 统计

matplotlib ×2

pandas ×1

python ×1

seaborn ×1