将 Plotly Express 跟踪添加到具有多个跟踪的图形对象 go.Figure() 中?

Chr*_*Sil 3 python-3.x plotly plotly.graph-objects

我正在尝试使用绘图对象来可视化来自不同 pandas 数据帧的时间戳。成功添加跟踪作为 go.Scatter() 图后,我现在想添加 px.timeline() 图作为跟踪。这一般可以吗?

这是我的代码:

import pandas as pd
import plotly.express as px
import plotly.graph_objects as go


df=pd.read_json(file1.json)
df2=pd.read_json(file2.json)
df3=pd.read_json(file3.json)

fig = go.Figure(layout=layout)
fig.add_trace(
    go.Scatter(x=df.column1, y=df.column2, name='name1', mode='markers')
)
fig.add_trace(
    go.Scatter(x=df2.column1, y=df2.column2, name='name2', mode='markers')
)
fig.add_trace(
    go.Scatter(x=df3.column1, y=df3.column2, name='name3', mode='markers')
)
#### add px trace ###
fig.update_traces(marker_size=10)

fig.show()
Run Code Online (Sandbox Code Playgroud)

到目前为止,这运作良好。现在,我想将 px.timeline() 图添加为我的图形的跟踪。我试过:

fig.add_trace(
    px.timeline(df,x_start="column1",
                x_end="column2", y='column3')
Run Code Online (Sandbox Code Playgroud)

这会引发值错误:

ValueError: 
    Invalid element(s) received for the 'data' property of [...]
Run Code Online (Sandbox Code Playgroud)

是否有解决方法可以将 px.-Plots 添加到 go.Figure() ?

Yas*_*sin 8

尝试包含.data[0]

fig.add_trace(
px.timeline(df,x_start="column1",
            x_end="column2", y='column3').data[0])
Run Code Online (Sandbox Code Playgroud)