0

我正在绘制有间隙的时间序列。观察来自 6 个不同的日期,每天连续数据点之间的滞后平均约为 10 秒。

在某些日子,观察窗口只是一天的一部分,例如从 00:00 到 7:40,这导致包含所有天的合并序列中存在巨大差距。我想绘制合并数据,但将天数留空。相反,现在有某种线性插值 - 在下面的图中,您可以看到数据的蓝色图和 x 轴值上方的红点,这些值确实存在于数据中:

在此处输入图像描述

我发现它具有误导性,例如有时间隙中的线在 y = 0 上,因此我最初认为该设备(这是功耗数据)在那个时期是不活动的并且 y 的值为 0(实际上我们只是不根本没有那个时期的数据)。

我能做些什么来防止 matplotlib 在间隙中绘制线条?这就是我创建蓝色图的方式:

dev_data.plot(kind = "line", ax = axes[0], legend = None, title = "Device data")

编辑:我发现有人有相反的问题。我希望我的情节看起来像这个问题,但我不知道我应该在我的代码中更改什么。

4

1 回答 1

0

Matplotlib 不会打印缺失的(NaN 或掩码)值,请参阅此演示

要使用它,您需要找到间隙(例如,时间戳之间的差异 > 10 秒)并在这些间隙内的时间戳处添加额外的 NaN 值。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

idx = np.concatenate((pd.date_range('2011-04-17', periods=200, freq='10S'),
                      pd.date_range('2011-04-18', periods=100, freq='10S'),
                      pd.date_range('2011-04-19', periods=300, freq='10S')))
s = pd.Series(np.random.rand(len(idx)), index=idx)

fig, (ax1,ax2) = plt.subplots(2, layout='constrained')
s.plot(ax=ax1, legend=None, title="Original without gaps")

# positions with gaps > 10 s
gaps = np.flatnonzero(np.diff(s.index) > np.timedelta64(10, 's'))

# add empty values at gap start positions + 1 s
s1 = s.append(pd.Series(index=s[gaps].index + np.timedelta64(1, 's'), dtype=float))

s1.plot(ax=ax2, legend=None, title="With gaps")

在此处输入图像描述

于 2021-12-17T14:26:42.777 回答