在 Plotly 上绘制 df 行

Plot rows of df on Plotly

df是这样的:

          X             Y            Label
0  [16, 37, 38]  [7968, 4650, 3615]   0.7
1  [29, 37, 12]  [4321, 4650, 1223]   0.8
2  [12, 2, 445]  [1264, 3456, 2112]   0.9

这应该在同一个图上绘制三条线,并将标签作为连续变量。使用 plotly 绘制它的最快和最简单的方法是什么?

这应该在同一图上绘制三条线作为要求。 (这与 我想要 df 的每一行的子图不一致

为每一行创建轨迹的简单案例,使用https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.explode.html准备x和y

import pandas as pd
import plotly.graph_objects as go

df = pd.DataFrame(
    {
        "X": [[16, 37, 38], [29, 37, 12], [12, 2, 445]],
        "Y": [[7968, 4650, 3615], [4321, 4650, 1223], [1264, 3456, 2112]],
        "Label": [0.7, 0.8, 0.9],
    }
)

go.Figure(
    [
        go.Scatter(
            x=r["X"].explode(), y=r["Y"].explode(), name=str(r["Label"].values[0])
        )
        for _, r in df.groupby(df.index)
    ]
)

具有由 label

定义的连续颜色
import pandas as pd
import plotly.graph_objects as go
from plotly.colors import sample_colorscale
import plotly.express as px

df = pd.DataFrame(
    {
        "X": [[16, 37, 38], [29, 37, 12], [12, 2, 445]],
        "Y": [[7968, 4650, 3615], [4321, 4650, 1223], [1264, 3456, 2112]],
        "Label": [0.1, 0.5, 0.9],
    }
)

fig = px.scatter(x=[0], y=[0], color=[.5], color_continuous_scale="YlGnBu")


fig = fig.add_traces(
    [
        go.Scatter(
            x=r["X"].explode(),
            y=r["Y"].explode(),
            name=str(r["Label"].values[0]),
            line_color=sample_colorscale("YlGnBu", r["Label"].values[0])[0],
            showlegend=False
        )
        for _, r in df.groupby(df.index)
    ]
)

fig