.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples_mesh/example_construct_mesh.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_mesh_example_construct_mesh.py: ================================================================================ Construct a Mesh from a .msh file ================================================================================ Mesh is constructed from a `.msh` and plotted. .. GENERATED FROM PYTHON SOURCE LINES 9-15 .. code-block:: default import matplotlib.pyplot as plt import numpy as np import freshkiss3d as fk import freshkiss3d.extra.plots as fk_plt .. GENERATED FROM PYTHON SOURCE LINES 16-22 Read a Mesh -------------------- There is two ways to visualize a mesh from a file. You can either build a TriangularMesh or directly use the read_msh function. .. GENERATED FROM PYTHON SOURCE LINES 23-26 .. code-block:: default TG, vertex_labels, boundary_labels = fk.read_msh('inputs/tiny.msh') .. GENERATED FROM PYTHON SOURCE LINES 27-32 .. warning:: The read_msh function is compatible with ``.msh``, ``.msh`` (GMSH) and ``.mesh`` (MEDIT) formats ONLY. Note that the ``.msh`` used in freshkiss3d isn't the same as gmsh ``.msh`` format. .. GENERATED FROM PYTHON SOURCE LINES 35-38 Construct a TriangularMesh ------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 39-46 .. code-block:: default triangular_mesh = fk.TriangularMesh.from_msh_file('inputs/tiny.msh') TG = triangular_mesh.triangulation vertex_labels = triangular_mesh.vertex_labels boundary_edges = triangular_mesh.boundary_edges .. GENERATED FROM PYTHON SOURCE LINES 47-51 .. note:: When you build a TriangularMesh, the read_msh function is called in the background .. GENERATED FROM PYTHON SOURCE LINES 54-60 Basic plot -------------------- To visualize the mesh with triplot you need to recover nodes coordonates (x,y) and triangles (trivtx) .. GENERATED FROM PYTHON SOURCE LINES 61-69 .. code-block:: default x = np.asarray(TG.x) y = np.asarray(TG.y) trivtx = np.asarray(TG.trivtx) plt.triplot(x, y, trivtx) plt.show() .. image-sg:: /auto_examples_mesh/images/sphx_glr_example_construct_mesh_001.png :alt: example construct mesh :srcset: /auto_examples_mesh/images/sphx_glr_example_construct_mesh_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 70-77 Fancy plot -------------------- Basic plot isn't really helpfull when you need specific information about your mesh, for example node or boundary labels. So here is shown how to add life to your mesh plots: .. GENERATED FROM PYTHON SOURCE LINES 78-107 .. code-block:: default colors = {0:'blue', 1:'red', 2:'green'} plt.figure(figsize=(8, 8)) # Plot triangles. plt.triplot(x, y, trivtx) xtri = np.average(x[trivtx], axis=1) ytri = np.average(y[trivtx], axis=1) fk_plt.put_text_index(xtri, ytri, color='red') # Plot points. for a, b, label in zip(x, y, vertex_labels): plt.plot(a, b, marker='o', color=colors[label], markersize=10) fk_plt.put_text_index(x, y, offset=(0.04, 0.04)) # Plot edges. for B in range(boundary_edges.size): i0 = boundary_edges.vertices[B, 0] i1 = boundary_edges.vertices[B, 1] x0, y0 = x[i0], y[i0] x1, y1 = x[i1], y[i1] plt.plot([x0, x1], [y0, y1], color=colors[boundary_edges.label[B]], linewidth=3.) plt.grid() plt.axis('scaled') plt.xlim(-0.5, 6.5) plt.ylim(9.5, 15.5) plt.show() .. image-sg:: /auto_examples_mesh/images/sphx_glr_example_construct_mesh_002.png :alt: example construct mesh :srcset: /auto_examples_mesh/images/sphx_glr_example_construct_mesh_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.295 seconds) .. _sphx_glr_download_auto_examples_mesh_example_construct_mesh.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: example_construct_mesh.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: example_construct_mesh.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_