You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在DataFrame中按时间间隔条件插入含NaN值的新行以中断Plotly连线

解决Plotly折线图在数据间隔过大时自动连线的问题

方案一:插入NaN行实现折线中断

如果你需要通过插入NaN行的方式让Plotly断开连线,可以用以下代码实现:

先确保datetime列是pandas的datetime类型,计算相邻行的时间差后,在超过阈值(15分15秒=915秒)的位置插入含NaN值的行:

import pandas as pd

# 原始数据
data = {
    'datetime': ['2022-03-09 09:25:00', '2022-03-09 09:30:00', '2022-03-09 09:51:00', '2022-03-09 09:54:00'],
    'value': [98, 104, 105, 110]
}
df = pd.DataFrame(data)
df['datetime'] = pd.to_datetime(df['datetime'])

# 定义时间阈值(15分15秒,单位秒)
threshold = 15*60 + 15

# 计算相邻行的时间差(秒)
df['time_diff_sec'] = (df['datetime'].shift(-1) - df['datetime']).dt.total_seconds()

# 筛选出需要插入间隔的行索引
gap_indices = df[df['time_diff_sec'] > threshold].index

# 逆序插入NaN行,避免索引错乱
inserted_rows = []
for idx in gap_indices:
    current_time = df.loc[idx, 'datetime']
    next_time = df.loc[idx+1, 'datetime']
    # 取两段时间的中点作为插入行的时间
    mid_time = current_time + (next_time - current_time)/2
    new_row = pd.DataFrame({'datetime': [mid_time], 'value': [pd.NA]})
    inserted_rows.append((idx+1, new_row))

for pos, row in reversed(inserted_rows):
    df = pd.concat([df.iloc[:pos], row, df.iloc[pos:]]).reset_index(drop=True)

# 清理辅助列
df = df.drop('time_diff_sec', axis=1)

# 输出处理后的数据集
print(df)

处理后的DataFrame会在间隔过大的位置插入NaN行,之后用Plotly绘图时设置connectgaps=False即可断开连线。

方案二:分割数据为多个Trace(更高效)

插入行的方式会增加数据量,大数据场景下不够高效。更优的做法是将数据按时间间隔分割成多个连续分组,每个分组作为独立的Plotly Trace,不同分组之间自然不会连线:

import plotly.express as px
import pandas as pd

# 原始数据处理
data = {
    'datetime': ['2022-03-09 09:25:00', '2022-03-09 09:30:00', '2022-03-09 09:51:00', '2022-03-09 09:54:00'],
    'value': [98, 104, 105, 110]
}
df = pd.DataFrame(data)
df['datetime'] = pd.to_datetime(df['datetime'])

threshold = 15*60 + 15  # 915秒

# 给连续的数据段标记分组
df['group'] = 0
current_group = 0
for i in range(1, len(df)):
    time_diff = (df.loc[i, 'datetime'] - df.loc[i-1, 'datetime']).total_seconds()
    if time_diff > threshold:
        current_group += 1
    df.loc[i, 'group'] = current_group

# 绘制折线图,每个分组一个Trace,统一颜色并隐藏图例
fig = px.line(df, x='datetime', y='value', color='group',
              color_discrete_sequence=['#1f77b4']*df['group'].nunique(),
              markers=True)

fig.update_layout(showlegend=False)
# 确保每个Trace内部不会连接NaN(此处可选,但加上更稳妥)
fig.update_traces(connectgaps=False)

fig.show()

这种方式无需修改原始数据结构,运行效率更高,最终图表效果和插入NaN行完全一致。


内容的提问来源于stack exchange,提问作者walker967

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 18:16:04