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

Plotly处理6万条长时序数据时高亮周末及工作时段的高效方案

性能问题核心原因

  • 原始代码逐行遍历6万条分钟级数据,大量重复判断同一日期/时段的规则,冗余度极高
  • 循环内反复调用fig.add_shape()接口,每次调用都会修改绘图对象的全局结构,调用数万次开销极大

优化方案

通过去重时间维度、批量传参的方式优化,修改后运行时间可降至秒级,完整代码如下:

import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots

# 第一步:提取去重后的时间维度,避免冗余判断
# 仅保留唯一日期(用于周五判断)和唯一小时点(用于工作时段判断)
unique_dates = fl15['time'].dt.date.drop_duplicates().values
unique_hours = fl15['time'].dt.floor('h').drop_duplicates().values

# 第二步:批量生成所有高亮形状配置,不要逐次调用add_shape
shapes = []
# 处理周五灰色高亮
for d in unique_dates:
    dt = pd.to_datetime(d)
    if dt.weekday() == 4:
        shapes.append({
            "type": "rect",
            "xref": "x",
            "yref": "paper",
            "x0": dt,
            "y0": 0,
            "x1": dt + pd.DateOffset(1),
            "y1": 1,
            "line": {"color": "rgba(0,0,0,0)", "width": 3},
            "opacity": 0.2,
            "fillcolor": "rgba(189, 186, 183, 1)",
            "layer": "below"
        })
# 处理非周五6-18点蓝色高亮
for h in unique_hours:
    if h.weekday() != 4 and h.hour == 6:
        shapes.append({
            "type": "rect",
            "xref": "x",
            "yref": "paper",
            "x0": h,
            "y0": 0,
            "x1": h + pd.DateOffset(hours=12),
            "y1": 1,
            "line": {"color": "rgba(0,0,0,0)", "width": 3},
            "opacity": 0.2,
            "fillcolor": "rgba(135, 190, 238, 1)",
            "layer": "below"
        })

# 第三步:绘图并批量传入形状配置
fig = make_subplots(rows=1, cols=1)
fig.add_scatter(x=fl15['time'], y=fl15['ThreePhaseElectricityMeasurement.Active_power_Total_Minute_Max'], mode='lines', name='Floor 15')
fig.add_scatter(x=fl16['time'], y=fl16['ThreePhaseElectricityMeasurement.Active_power_Total_Minute_Max'], mode='lines', name='Floor 16')
fig.add_scatter(x=fl17['time'], y=fl17['ThreePhaseElectricityMeasurement.Active_power_Total_Minute_Max'], mode='lines', name='Floor 17')
fig.add_hrect(y0=3146, y1=4836, line_width=0, fillcolor="yellow", opacity=0.35)

fig.update_layout(
    title="Minute Max Power Vs. Time",
    xaxis_title="Time",
    yaxis_title="Minute Max Power (W)",
    shapes=shapes
)
        
fig.show()

扩展说明

如果需要支持周末、其他工作时段高亮,仅需调整对应日期、小时的判断规则即可,核心逻辑无需修改。

内容的提问来源于stack exchange,提问作者A.Shoman

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 11:39:03