如何向时间序列数据的 Pandas 条形图添加一条垂直线 [英] How to add a vertical line to a pandas bar plot of time-series data

查看:42
本文介绍了如何向时间序列数据的 Pandas 条形图添加一条垂直线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Pandas 并假设我们有以下数据帧:

i used Pandas and supposed we have the following DataFrame :

ax = madagascar_case[["Ratio"]].loc['3/17/20':]
ax.tail()

出:

我想在比率值之后显示条形图并添加与特定日期相关的垂直线,例如:'4/20/20':

i would like to show a bar chart following ratio values and add a vertical line related to a specific date for instance : '4/20/20' :

当我尝试下面的代码时:

when I try the code below :

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")

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

the result is the vertical line (red) is at the wrong date and there is no label :

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

So to fix that I try another code by using 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()

结果比预期的要糟糕.:

The result was worse than expected. :

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

RVA92 >> I followed your last code :

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()

结果是当我将日期更改为4/9/20"时它有效,但是当我将日期更改为4/20/20"时它不适合我不知道为什么?

the result is it works when I change the date to '4/9/20' for example , but when I change the date to '4/20/20' it doesn't fit correctly I don't know why ?

ax.axvline(df.index.searchsorted('4/20/20'), color="red", linestyle="--", lw=2, label="lancement")

推荐答案

  • 您可以使用给定日期的索引号来绘制一条垂直线,
    • df.index.searchsorted('3/20/20') 返回给定日期的索引号.
      • You can use the index number for a given date, to plot a vertical line,
        • df.index.searchsorted('3/20/20') returns the index number for the given date.
        • # Import libraries
          import pandas as pd
          import matplotlib.pyplot as plt
          
          # Create test data
          madagascar_case = pd.DataFrame(data={'Ratio': [0.5, 0.7, 0.8, 0.9]}, index=['3/19/20', '3/20/20', '3/21/20', '3/22/20'])
          
          # Choose subset of data
          df = madagascar_case.loc['3/19/20':, 'Ratio'].copy()
          
          # Set up figure
          fig, ax = plt.subplots()
          
          # Plot bars
          df.plot.bar(figsize=(17, 7), grid=True, ax=ax)
          
          # Plot vertical lines
          ax.axvline(df.index.searchsorted('3/20/20'), color="red", linestyle="--", lw=2, label="lancement")
          ax.axvline(df.index.searchsorted('3/22/20'), color="red", linestyle="--", lw=2, label="lancement")
          

          这篇关于如何向时间序列数据的 Pandas 条形图添加一条垂直线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆