向时间序列的熊猫df.plot添加垂直线

时间:2020-01-29 13:21:24

标签: pandas python-2.7 datetime plot time-series

我有一个时序图,我想在event time上添加一条垂直线。如果我使用此代码:

event_time = pd.to_datetime('10/12/2016 06:21:00')
ax = df_stats_t.plot(x = 't', y='t_TI_var_pwr', linestyle='none',...
color = 'black', marker = 'o')
ax1 = ax.twinx()
ax1.axvline(event_time, color='red', linestyle='-')
df_stats_t.plot(x='t',y='t_TI_var_ws',ax=ax1, linestyle='none', ...
color = 'green', marker = 'o')

它需要一个从event_time开始的时间序列的子集,并且不会产生垂直线。

enter image description here

如果将ax1.axvline(event_time, color='red', linestyle='-')移到底部,我会得到想要的绘图,但垂直线仍然丢失。

    event_time = pd.to_datetime('10/12/2016 06:21:00')
    ax = df_stats_t.plot(x = 't', y='t_TI_var_pwr', linestyle='none',... 
    color = 'black', marker = 'o')
    ax1 = ax.twinx()
    df_stats_t.plot(x='t',y='t_TI_var_ws',ax=ax1, linestyle='none',... 
    color = 'green', marker = 'o')
    ax1.axvline(event_time, color='red', linestyle='-')

enter image description here

如何获取所有y值在x = event_time播放的垂直线?

1 个答案:

答案 0 :(得分:0)

plt

一起使用
ax = df_stats_t.plot(x = 't', y='t_TI_var_pwr', linestyle='none', color = 'black', marker = 'o')
ax1 = ax.twinx()
df_stats_t.plot(x='t',y='t_TI_var_ws',ax=ax1, linestyle='none', color = 'green', marker = 'o')
plt.axvline(event_time, color='red', linestyle='-')