.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples\00-read_binary\custom_visualization.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_00-read_binary_custom_visualization.py: .. _ref_custom_visualization: Custom Scalar Visualization ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Display custom scalars using an existing mesh. .. GENERATED FROM PYTHON SOURCE LINES 9-17 .. code-block:: Python import numpy as np from ansys.mapdl.reader import examples # Download an example shaft modal analysis result file shaft = examples.download_shaft_modal() .. GENERATED FROM PYTHON SOURCE LINES 18-22 Each result file contains both a ``mesh`` property and a ``grid`` property. The ``mesh`` property can be through as the MAPDL representation of the FEM while the ``grid`` property can be through of the Python visualizing property used to plot within Python. .. GENERATED FROM PYTHON SOURCE LINES 22-27 .. code-block:: Python print("shaft.mesh:\n", shaft.mesh) print("-" * 79) print("shaft.grid:\n", shaft.grid) .. rst-class:: sphx-glr-script-out .. code-block:: none shaft.mesh: ANSYS Mesh Number of Nodes: 27132 Number of Elements: 25051 Number of Element Types: 6 Number of Node Components: 4 Number of Element Components: 3 ------------------------------------------------------------------------------- shaft.grid: UnstructuredGrid (0x249b3475120) N Cells: 25051 N Points: 27132 X Bounds: 0.000e+00, 2.700e+02 Y Bounds: -4.000e+01, 4.000e+01 Z Bounds: -4.000e+01, 4.000e+01 N Arrays: 16 .. GENERATED FROM PYTHON SOURCE LINES 28-35 Plotting ~~~~~~~~ The grid instance is a :class:`pyvista.UnstructuredGrid` part of the `pyvista `_ library. This class allows for advanced plotting using VTK in just a few lines of code. For example, you can plot the underlying mesh with: .. GENERATED FROM PYTHON SOURCE LINES 35-38 .. code-block:: Python shaft.grid.plot(color="w", smooth_shading=True) .. image-sg:: /examples/00-read_binary/images/sphx_glr_custom_visualization_001.png :alt: custom visualization :srcset: /examples/00-read_binary/images/sphx_glr_custom_visualization_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 39-50 Plotting Node Scalars ~~~~~~~~~~~~~~~~~~~~~ If you point-wise or cell-wise scalars (nodes and elements in FEA), you can plot these scalars by setting the ``scalars=`` parameter. Here, I'm simply using the x location of the nodes to color the mesh. It follows that you can use any set of scalars provided that it matches the number of nodes in the unstructured grid or the number of cells in the unstructured grid. Here, we're plotting node values. .. GENERATED FROM PYTHON SOURCE LINES 50-55 .. code-block:: Python x_scalars = shaft.grid.points[:, 0] shaft.grid.plot(scalars=x_scalars, smooth_shading=True) .. image-sg:: /examples/00-read_binary/images/sphx_glr_custom_visualization_002.png :alt: custom visualization :srcset: /examples/00-read_binary/images/sphx_glr_custom_visualization_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 56-69 Plotting With Missing Values ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ If you do not have values for every node (for example, the midside nodes), you can leave these values as NAN and the plotter will take care of plotting only the real values. For example, if you have calculated strain scalars that are only available at certain nodes, you can still plot those. This example just nulls out the first 2000 nodes to be able to visualize the missing values. If your are just missing midside values, your plot will not show the missing values since `pyvista` only plots the edge nodes. .. GENERATED FROM PYTHON SOURCE LINES 69-77 .. code-block:: Python pontoon = examples.download_pontoon() nnum, strain = pontoon.nodal_elastic_strain(0) scalars = strain[:, 0] scalars[:2000] = np.nan # here, we simulate unknown values pontoon.grid.plot(scalars=scalars, show_edges=True, lighting=False) .. image-sg:: /examples/00-read_binary/images/sphx_glr_custom_visualization_003.png :alt: custom visualization :srcset: /examples/00-read_binary/images/sphx_glr_custom_visualization_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.732 seconds) .. _sphx_glr_download_examples_00-read_binary_custom_visualization.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: custom_visualization.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: custom_visualization.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_