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

如何基于Mode分组绘制Plotly散点图展示Ch1-Ch3随Date的变化

按 Mode 分组绘制散点图的解决方案

以下是两种实现按 Mode(A/B/C)分组绘制散点图的方法,基于你提供的代码修改:


方法一:每个 Mode + Channel 组合单独作为一条轨迹

这种方法为每个通道(Ch1/Ch2/Ch3)和 Mode 的组合创建独立散点轨迹,便于在图例中区分不同 Mode 的数据点。

import plotly.graph_objs as go

ch_columns = ['Ch1', 'Ch2', 'Ch3']
unique_modes = dff['Mode'].unique()  # 获取所有唯一的 Mode 值
channels = []

# 遍历每个 Mode 和通道组合
for mode in unique_modes:
    mode_df = dff[dff['Mode'] == mode]  # 筛选当前 Mode 的数据
    for ch in ch_columns:
        channel_trace = go.Scatter(
            name=f"{ch} - Mode {mode}",
            x=mode_df['Date'],
            y=mode_df[ch],
            text=mode_df['Serial'],
            mode='markers',
            marker=dict(size=10)  # 可调整点的大小
        )
        channels.append(channel_trace)

layout = dict(
    title='Channels / Time (Grouped by Mode)',
    xaxis=dict(
        type='date',
        title='Date',
        tickformat='%Y/%m/%d',
        ticklen=5,
        rangeslider=dict(visible=True)
    ),
    yaxis=dict(title='Channel Values'),
    legend=dict(title='Channel + Mode')  # 图例标题
)

fig = dict(data=channels, layout=layout)
fig.show()

方法二:按 Mode 为通道数据点着色

这种方法将同一通道的不同 Mode 数据点用不同颜色区分,保持每个通道为一条轨迹,同时通过颜色编码 Mode 信息。

import plotly.graph_objs as go

ch_columns = ['Ch1', 'Ch2', 'Ch3']
# 定义 Mode 对应的颜色映射
mode_colors = {'A': '#FF5733', 'B': '#33FF57', 'C': '#3357FF'}
channels = []

for ch in ch_columns:
    channel_trace = go.Scatter(
        name=ch,
        x=dff['Date'],
        y=dff[ch],
        text=dff['Serial'],
        mode='markers',
        marker=dict(
            color=dff['Mode'].map(mode_colors),  # 根据 Mode 分配颜色
            size=10,
            # 扩展悬停信息,显示 Mode
            hovertext=dff.apply(lambda row: f"Serial: {row['Serial']}<br>Mode: {row['Mode']}", axis=1)
        )
    )
    channels.append(channel_trace)

# 添加 Mode 颜色图例(创建辅助轨迹)
mode_legend_traces = []
for mode, color in mode_colors.items():
    mode_legend_traces.append(go.Scatter(
        x=[None], y=[None],
        mode='markers',
        marker=dict(color=color, size=10),
        name=f"Mode {mode}"
    ))

layout = dict(
    title='Channels / Time (Colored by Mode)',
    xaxis=dict(
        type='date',
        title='Date',
        tickformat='%Y/%m/%d',
        ticklen=5,
        rangeslider=dict(visible=True)
    ),
    yaxis=dict(title='Channel Values'),
    legend=dict(title='Channels & Modes')
)

# 合并通道轨迹和 Mode 图例轨迹
fig = dict(data=channels + mode_legend_traces, layout=layout)
fig.show()

说明

  • 方法一适合单独查看每个 Mode 下各通道数据分布的场景,图例清晰区分每个组合。
  • 方法二更适合对比同一通道内不同 Mode 的数据差异,通过颜色快速识别 Mode。
  • 两种方法都保留了你原有的 Serial 悬停信息,方便查看每个数据点的序列号。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 07:23:13