How to Combine graph_objects with figure_factory?

Hi everyone

How can I combine figure_factory figure with graph_objects figure? Here is a sample But I can’t combine them to be plotted in a figure.

import plotly.graph_objects as go
import plotly.figure_factory as ff
import numpy as np
from scipy.spatial import Delaunay


fig=go.Figure()

x=[1,-1,-1,0,1]
y=[0,1,1,0.5,0]
z=[0,0,1,2,1]

points=[(i,j) for i,j in zip(x,y)]
fig.add_scatter3d(x=x,y=y,z=z)

u = np.linspace(0, 2*np.pi, 24)
v = np.linspace(-1, 1, 8)
u,v = np.meshgrid(u,v)
u = u.flatten()
v = v.flatten()

tp = 1 + 0.5*v*np.cos(u/2.)
x = tp*np.cos(u)
y = tp*np.sin(u)
z = 0.5*v*np.sin(u/2.)

points2D = np.vstack([u,v]).T
tri = Delaunay(points2D)
simplices = tri.simplices

figf = ff.create_trisurf(x=x, y=y, z=z,
                         colormap="Portland",
                         simplices=simplices,
                         title="Mobius Band")
figf.show()

I don’t want to use Mesh3D and need to use trisurf.

Hi everyone

I found the solution and put it here maybe be helpful for someone in future:

import plotly.graph_objects as go
import plotly.figure_factory as ff
import numpy as np
from scipy.spatial import Delaunay


fig=go.Figure()

x=[1,-1,-1,0,1]
y=[0,1,1,0.5,0]
z=[0,0,1,2,1]

points=[(i,j) for i,j in zip(x,y)]
fig.add_scatter3d(x=x,y=y,z=z)

u = np.linspace(0, 2*np.pi, 24)
v = np.linspace(-1, 1, 8)
u,v = np.meshgrid(u,v)
u = u.flatten()
v = v.flatten()

tp = 1 + 0.5*v*np.cos(u/2.)
x = tp*np.cos(u)
y = tp*np.sin(u)
z = 0.5*v*np.sin(u/2.)

points2D = np.vstack([u,v]).T
tri = Delaunay(points2D)
simplices = tri.simplices

figf = ff.create_trisurf(x=x, y=y, z=z,
                         colormap="Portland",
                         simplices=simplices,
                         title="Mobius Band")

fig = go.Figure(data=[figf.data[0],figf.data[1],figf.data[2],fig.data[0]])

fig.show()

Another Better method also found in this way:

import plotly.graph_objects as go
import plotly.figure_factory as ff
import numpy as np
from scipy.spatial import Delaunay


fig=go.Figure()

x=[1,-1,-1,0,1]
y=[0,1,1,0.5,0]
z=[0,0,1,2,1]

points=[(i,j) for i,j in zip(x,y)]
fig.add_scatter3d(x=x,y=y,z=z)

u = np.linspace(0, 2*np.pi, 24)
v = np.linspace(-1, 1, 8)
u,v = np.meshgrid(u,v)
u = u.flatten()
v = v.flatten()

tp = 1 + 0.5*v*np.cos(u/2.)
x = tp*np.cos(u)
y = tp*np.sin(u)
z = 0.5*v*np.sin(u/2.)

points2D = np.vstack([u,v]).T
tri = Delaunay(points2D)
simplices = tri.simplices

figf = ff.create_trisurf(x=x, y=y, z=z,
                         colormap="Portland",
                         simplices=simplices,
                         title="Mobius Band")

fig.add_traces(figf.data)

fig.show()

Final Advice:

For Thousand reasons Do Not use figure_factory.create_trisurf and Use Mesh3D Instead

1 Like