Plot a mesh#
This tutorial shows different commands for plotting a mesh without data.
A mesh is represented in DPF by a MeshedRegion
object.
You can store multiple MeshedRegion
in a DPF collection called MeshesContainer
.
You can obtain a MeshedRegion
by creating your own from scratch or by getting it from a result file.
For more information, see the Create a mesh from scratch and
Get a mesh from a result file tutorials.
PyDPF-Core has a variety of plotting methods for generating 3D plots with Python. These methods use VTK and leverage the PyVista library.
Download tutorial as Python script
Download tutorial as Jupyter notebook
Load data to plot#
For this tutorial, we use mesh information from a case available in the Examples
module.
For more information see the Get a mesh from a result file tutorial.
# Import the ``ansys.dpf.core`` module
import ansys.dpf.core as dpf
# Import the examples module
from ansys.dpf.core import examples
# Import the operators module
from ansys.dpf.core import operators as ops
# Download and get the path to an example result file
result_file_path_1 = examples.download_piston_rod()
# Create a model from the result file
model_1 = dpf.Model(data_sources=result_file_path_1)
Plot a model#
You can directly plot the overall mesh loaded by the model with Model.plot()
[1].
# Plot the mesh
model_1.plot()

(None, <pyvista.plotting.plotter.Plotter at 0x290836ffdd0>)
Plot a single mesh#
Get the mesh#
Here we simply get the MeshedRegion
object of the model, but any other MeshedRegion
works.
# Extract the mesh
meshed_region_1 = model_1.metadata.meshed_region
Plot the mesh#
To plot the MeshedRegion
you can use:
The
MeshedRegion.plot()
method;The
DpfPlotter
object.
Use the MeshedRegion.plot()
method [1] of the MeshedRegion
object we defined.
# Plot the mesh object
meshed_region_1.plot()

(None, <pyvista.plotting.plotter.Plotter at 0x290813e6f00>)
To plot the mesh with this approach, first create an instance of DpfPlotter
[2].
Then, add the MeshedRegion
to the scene using the add_mesh()
method.
To render and show the figure based on the current state of the plotter object, use the show_figure()
method.
# Create a DpfPlotter instance
plotter_1 = dpf.plotter.DpfPlotter()
# Add the mesh to the scene
plotter_1.add_mesh(meshed_region=meshed_region_1)
# Display the scene
plotter_1.show_figure()

(None, <pyvista.plotting.plotter.Plotter at 0x290ffd2fe60>)
You can also plot data contours on a mesh. For more information, see Plot contours
Plot several meshes#
Build a collection of meshes#
There are different ways to obtain a MeshesContainer
.
You can for example split a MeshedRegion
using operators.
Here, we use the split_mesh
operator to split the mesh based on the material of each element.
This operator returns a MeshesContainer
with meshes labeled according to the criterion for the split.
In our case, each mesh has a ‘mat’ label.
For more information about how to get a split mesh, see the Split a mesh
and Extract a mesh in split parts tutorials.
# Split the mesh based on material property
meshes = ops.mesh.split_mesh(mesh=meshed_region_1, property="mat").eval()
# Show the result
print(meshes)
DPF Meshes Container
with 2 mesh(es)
defined on labels: body mat
with:
- mesh 0 {mat: 1, body: 1, } with 17281 nodes and 9026 elements.
- mesh 1 {mat: 2, body: 2, } with 17610 nodes and 9209 elements.
Plot the meshes#
Use the MeshesContainer.plot()
method [1] of the MeshesContainer
object we defined.
This method plots all the MeshedRegion
objects stored in the MeshesContainer
and colors them based on the property used to split the mesh.
# Plot the collection of meshes
meshes.plot()

(None, <pyvista.plotting.plotter.Plotter at 0x290dc1e65a0>)
You can also plot data on a collection of meshes. For more information, see Plot contours
Footnotes