python - 我有一个时间序列的体积图。想要将另一列数据框强加为虚线
问题描述
我有一个看起来像的熊猫 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 ,但这并没有帮助,因为它也绘制了零点。
任何帮助将不胜感激。谢谢
解决方案
要绘制垂直线,请使用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='--')
结果:
推荐阅读
- c# - 基于另一个列值的唯一 id 集合的 Linq 查询
- c++ - nan 输出由于 maclaurin 系列正弦扩展,控制台崩溃
- oauth-2.0 - OAuth2:究竟什么是“客户端”
- firebase - EmberFire 关系持久性
- php - 在 PHP 中访问长多维数组
- sql-server - DBCC SHRINKFILE with emptyfile generate filegroup is full 错误
- python - 查找位于特定主机名上的所有网站
- regex - 16 位数字的正则表达式
- gulp - Gulp 没有创建新目录
- javascript - 仅限 IE 11:ReactDOM.createPortal() 错误:对象作为 React 子项无效