【发布时间】:2021-03-14 05:38:00
【问题描述】:
我有以下时间序列,以ts 作为索引,列名为metric_value:
ts
2020-01-01 00:00:00 1225917
2020-01-01 01:00:00 670334
2020-01-01 02:00:00 668207
2020-01-01 03:00:00 576977
2020-01-01 04:00:00 713490
Name: metric_value, dtype: int32
我正在尝试绘制这个时间序列并用红色圆圈标记异常数据点。异常值的索引在此列表中:
idxs=['2020-06-06 19:00:00', '2020-07-04 19:00:00', '2020-08-08 19:00:00']
以下显示了我如何绘制六月的数据。
fig, ax = plt.subplots(1, 1, sharex="all", sharey="all", figsize=(12,4))
ax = ts.loc['2020-06-01 00:00:00':'2020-06-30 23:00:00']['metric_value'].plot(title='June')
ax = ts.loc[[idx for idx in idxs if idx>'2020-06-01 00:00:00' and idx<'2020-06-30 23:00:00']]['metric_value'].plot(style='.')
plt.xticks(rotation=45)
plt.ylim(bottom=0)
对于 6 月份,该指数有一个异常值=2020-06-06 19:00:00。问题是该图未在正确位置显示此数据点。它显示在第一个位置为零!我认为这是因为这两个图不共享 x 轴,并且该图仅显示第二个图的轴。我该如何解决?
我试过this solution,但没用!
【问题讨论】:
-
无法用提供的数据重现问题。您需要提供一个包含玩具数据集的Minimal, Complete, and Verifiable example(参考How to make good reproducible pandas examples)
标签: python pandas dataframe matplotlib time-series