Note
Go to the end to download the full example code.
Plain usage of the plotly backend#
This example shows the plain usage of the Plotly backend in the Visualization Interface Tool to plot different objects, including PyVista meshes, custom objects, and Plotly-specific objects.
from ansys.tools.visualization_interface.backends.plotly.plotly_interface import PlotlyBackend
from ansys.tools.visualization_interface.types.mesh_object_plot import MeshObjectPlot
from ansys.tools.visualization_interface import Plotter
import pyvista as pv
from plotly.graph_objects import Mesh3d
# Create a plotter with the Plotly backend
pl = Plotter(backend=PlotlyBackend())
# Create a PyVista mesh
mesh = pv.Sphere()
# Plot the mesh
pl.plot(mesh)
# Create a PyVista MultiBlock
multi_block = pv.MultiBlock()
multi_block.append(pv.Sphere(center=(-1, -1, 0)))
multi_block.append(pv.Cube(center=(-1, 1, 0)))
# Plot the MultiBlock
pl.plot(multi_block)
# Display the plotter
pl.show()
Now create a custom object#
class CustomObject:
def __init__(self):
self.name = "CustomObject"
self.mesh = pv.Cube(center=(1, 1, 0))
def get_mesh(self):
return self.mesh
def name(self):
return self.name
# Create a custom object
custom_cube = CustomObject()
custom_cube.name = "CustomCube"
# Create a MeshObjectPlot instance
mesh_object_cube = MeshObjectPlot(custom_cube, custom_cube.get_mesh())
# Plot the custom mesh object
pl.plot(mesh_object_cube)
Display the plotter again#
Since Plotly is a web-based visualization, we can show the plot again to include the new object.
pl.show()
Add a Plotly Mesh3d object directly#
custom_mesh3d = Mesh3d(
x=[0, 1, 2],
y=[0, 1, 0],
z=[0, 0, 1],
i=[0],
j=[1],
k=[2],
color='lightblue',
opacity=0.50
)
pl.plot(custom_mesh3d, name="CustomMesh3d")
# Show other plotly objects like Scatter3d
from plotly.graph_objects import Scatter3d
scatter = Scatter3d(
x=[0, 1, 2],
y=[0, 1, 0],
z=[0, 0, 1],
mode='markers',
marker=dict(size=5, color='red')
)
pl.plot(scatter, name="CustomScatter3d")
pl.show()
Total running time of the script: (0 minutes 0.085 seconds)