首页 > 解决方案 > 我有一个时间序列的体积图。想要将另一列数据框强加为虚线

问题描述

我有一个看起来像的熊猫 df:

         date total any_esc
0   2018-04-01  1   0.0
1   2018-04-02  1   0.0
2   2018-04-03  4   1.0
3   2018-04-04  5   1.0
4   2018-04-05  10  1.0

I plotted the timeseries as:
df.plot('date', 'total')

现在我想在情节上强加 any_esc。每当 any_esc = 1 时,我希望在该日期有一条垂直虚线。

我尝试在 y 轴上绘制 total 和 any_esc ,但这并没有帮助,因为它也绘制了零点。

任何帮助将不胜感激。谢谢

标签: pythonpandasdataframematplotlibplot

解决方案


要绘制垂直线,请使用axvline

# The main plot
ax = df.plot.line(x='date', y='total')

# The vertical lines
for _, row in df.query('any_esc == 1').iterrows():
    ax.axvline(row['date'], linestyle='--')

结果:

结果


推荐阅读