首页 > 解决方案 > 在 Matplotlib 散点图中突出显示数据间隙 (NaN)

问题描述

我正在 matplotlib 中绘制一些来自 pandas 的基于时间的数据(可能是数万行),我想突出显示数据中存在 NaN 的时段。我认为实现这一点的方法是使用 axvspan 在有数据间隙的地方开始和停止绘图上绘制一个红色框。我确实考虑过每次使用 axvline 有一个 NaN 时只画一条垂直线,但这可能会在绘图上创建数千个对象并导致生成的 PNG 需要很长时间才能写入。所以我认为使用 axvspan 更合适。但是,我遇到的困难是找到 NaN 组的开始和停止索引。

下面的代码不是来自我的实际代码,它只是一个基本模型,用于显示我想要实现的目标。

import pandas as pd
import numpy as np
from datetime import datetime, timedelta
import matplotlib.pyplot as plt

days = pd.date_range(datetime.now(), datetime.now() + timedelta(13), freq='D')
data = [2,2.3,3,np.nan, np.nan,4.7,3.4,3.1,2.7,np.nan,np.nan,np.nan,4,4.5]
df = pd.DataFrame({'idx': days, 'col': data})
df = df.set_index('idx')
print(df)

#Code to find the start index and stop index of the groups of NaNs
# resuls in list which contains lists of each gap start and stop datetime
gaps = []

plt.plot(df.index, df['col'])

for gap in gaps: 
    plt.axvspan(gap[0], gap[1], facecolor='r', alpha=0.5)

plt.show()

结果看起来像下面的模型: 在此处输入图像描述

其他可视化间隙的建议也将不胜感激。比如一条不同颜色的直线使用某种填充连接跨越间隙的数据?

标签: pythonpython-3.xpandasnumpymatplotlib

解决方案


col要查找 NaN 组的开始和停止索引,您可以首先创建一个变量来保存is的布尔值NaNvalid使用此变量,您可以找到在和NaN值之间存在转换的行。这可以使用shift(在数据帧上移动一行) 和来完成ne,这样您可以比较两个连续的行并确定值在哪里交替。之后,申请cumsum创建不同的连续数据组validNaN值。

现在,仅使用具有NaN值 ( df[is_nan]) 的行使用groupbywithn_groups来收集同一组内的间隙。接下来,应用aggregate返回单个元组,其中包含每个组的开始和结束时间戳。这里的用途DateOffset是将矩形显示扩展到所需图像输出之后的相邻点。您现在可以使用['col'].values访问返回的数据框aggregate并将其转换为列表。

...
...
df = df.set_index('idx')
print(df)

# Code to find the start index and stop index of the groups of NaNs
is_nan = df['col'].isna()
n_groups = is_nan.ne(is_nan.shift()).cumsum()
gap_list = df[is_nan].groupby(n_groups).aggregate(
    lambda x: (
        x.index[0] + pd.DateOffset(days=-1),
        x.index[-1] + pd.DateOffset(days=+1)
    )
)["col"].values

# resuls in list which contains tuples of each gap start and stop datetime
gaps = gap_list

plt.plot(df.index, df['col'], marker='o' )
plt.xticks(df.index, rotation=45)

for gap in gaps:
    plt.axvspan(gap[0], gap[1], facecolor='r', alpha=0.5)

plt.grid()
plt.show()

plot_nan_gaps


推荐阅读