Is it possible to add a express figure as a trace to a graph-objects figure, or vice versa?

For example, adding the following px.scatter_3d figure as a trace to the graph-objects.Scatter3d figure, or the other way around. Is this two types of charts compatible to each other?

{source of code: plotly 3d scatter plots}

plotly express scatter 3d figure:

import plotly.express as px
df = px.data.iris()
fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width',
              color='petal_length', size='petal_length', size_max=18,
              symbol='species', opacity=0.7)

# tight layout
fig.update_layout(margin=dict(l=0, r=0, b=0, t=0))

plotly graph-objects scatter3d figure:

import plotly.graph_objects as go
import numpy as np

# Helix equation
t = np.linspace(0, 10, 50)
x, y, z = np.cos(t), np.sin(t), t

fig = go.Figure(data=[go.Scatter3d(x=x, y=y, z=z,
                                   mode='markers')])
fig.show()

Hi @oat,

px.scatter ist just a helper function that uses go.Scatter under the hood. You can add the traces from the first figure to the second (see below) :wink:

import plotly.express as px
import plotly.graph_objects as go
import numpy as np

df = px.data.iris()
fig1 = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width',
              color='petal_length', size='petal_length', size_max=18,
              symbol='species', opacity=0.7)

# tight layout
fig1.update_layout(margin=dict(l=0, r=0, b=0, t=0))

# Helix equation
t = np.linspace(0, 10, 50)
x, y, z = np.cos(t), np.sin(t), t

fig2 = go.Figure(data=[go.Scatter3d(x=x, y=y, z=z,
                                   mode='markers')])
fig2.add_traces(fig1.data)

fig2.show()

hope it helps, Alex

1 Like

Thank you very much, @Alexboiboi, for the example.