.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/howto_postprocessing/plot_sample_mesh_line.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_auto_examples_howto_postprocessing_plot_sample_mesh_line.py: ***************************** Plot data of a sampling lines ***************************** .. sectionauthor:: Florian Zill (Helmholtz Centre for Environmental Research GmbH - UFZ) This example provides clean coding recipes for plotting data of meshes over sampling lines. We also present different ways to setup the sampling lines. For plotting we us the function :py:func:`ogstools.plot.line`. .. GENERATED FROM PYTHON SOURCE LINES 14-28 .. code-block:: Python from itertools import pairwise import matplotlib.pyplot as plt import numpy as np import pyvista as pv import ogstools as ot from ogstools import examples ot.plot.setup.show_region_bounds = False mesh = examples.load_mesh_mechanics_2D() .. GENERATED FROM PYTHON SOURCE LINES 34-39 Simple case: straight line ========================== We use the ``pyvista`` function ``sample_over_line`` and use two points to define the line and get a Mesh with the sampled data. Let's plot the Mesh and the line together. .. GENERATED FROM PYTHON SOURCE LINES 41-45 .. code-block:: Python sample = mesh.sample_over_line([25, -460, 0], [100, -800, 0]) fig = mesh.plot_contourf(ot.variables.temperature) fig = ot.plot.line(sample, ax=fig.axes[0], linestyle="--") .. image-sg:: /auto_examples/howto_postprocessing/images/sphx_glr_plot_sample_mesh_line_001.png :alt: plot sample mesh line :srcset: /auto_examples/howto_postprocessing/images/sphx_glr_plot_sample_mesh_line_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 46-48 Now we plot the temperature data. The spatial coordinate for the x-axis is automatically detected here. .. GENERATED FROM PYTHON SOURCE LINES 50-54 .. code-block:: Python fig = ot.plot.line(sample, ot.variables.temperature) fig.tight_layout() .. image-sg:: /auto_examples/howto_postprocessing/images/sphx_glr_plot_sample_mesh_line_002.png :alt: plot sample mesh line :srcset: /auto_examples/howto_postprocessing/images/sphx_glr_plot_sample_mesh_line_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 55-60 Simple case: circular arc ========================= With 3 points we can define an arc over which to sample the data. Having the arc directly on a boundary might result in some gaps in the sampled data, thus we extend the arc by a small margin. .. GENERATED FROM PYTHON SOURCE LINES 62-73 .. code-block:: Python sample = mesh.sample_over_circular_arc( pointa=[100 - 1e-4, -650, 0], pointb=[200 + 1e-4, -650, 0], center=[150, -650, 0], ) fig, axs = plt.subplots(ncols=2, figsize=[26, 10]) mesh.plot_contourf(ot.variables.displacement["x"], fig=fig, ax=axs[1]) ot.plot.line(sample, ax=axs[1], lw=3, color="red") ot.plot.line(sample, ot.variables.displacement["x"], ax=axs[0]) fig.tight_layout() .. image-sg:: /auto_examples/howto_postprocessing/images/sphx_glr_plot_sample_mesh_line_003.png :alt: plot sample mesh line :srcset: /auto_examples/howto_postprocessing/images/sphx_glr_plot_sample_mesh_line_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 74-77 Other methods to setup the sampling line ======================================== The following section shows different methods of creating sampling lines. .. GENERATED FROM PYTHON SOURCE LINES 79-82 Linear spaced points -------------------- This basically does the same as the ``pyvista`` function `sample_over_line`. .. GENERATED FROM PYTHON SOURCE LINES 84-87 .. code-block:: Python pts = np.linspace([50, -460, 0], [50, -800, 0], 100) sample_1 = pv.PolyData(pts).sample(mesh) .. GENERATED FROM PYTHON SOURCE LINES 88-92 Mutilsegmented line from list of points --------------------------------------- The following code allows you to have a line connecting multiple observation points. .. GENERATED FROM PYTHON SOURCE LINES 94-98 .. code-block:: Python obs_pts = np.asarray([[150, -460, 0], [50, -650, 0], [150, -800, 0]]) pts = np.vstack([np.linspace(pt1, pt2, 50) for pt1, pt2 in pairwise(obs_pts)]) sample_2 = pv.PolyData(pts).sample(mesh) .. GENERATED FROM PYTHON SOURCE LINES 99-104 Spline from list of points -------------------------- You can also create smooth sampling lines by using a fitting function. The following creates a second order polynomial fit for the x-coordinates in dependence of the y-coordinates. .. GENERATED FROM PYTHON SOURCE LINES 106-112 .. code-block:: Python pts = np.asarray([[200, -460, 0], [250, -650, 0], [200, -800, 0]]) fit = np.poly1d(np.polyfit(pts[:, 1], pts[:, 0], 2)) y = np.linspace(-460, -800, 100) pts = np.transpose([fit(y), y, y * 0]) sample_3 = pv.PolyData(pts).sample(mesh) .. GENERATED FROM PYTHON SOURCE LINES 113-119 Use existing geometry --------------------- Another way to setup the sampling line is to extract points from the domain mesh. Here, we use the ``clip`` function from ``pyvista`` and some boolean logic, to extract a vertical line through the center, which follows the boundary of the hole. .. GENERATED FROM PYTHON SOURCE LINES 121-127 .. code-block:: Python edges = mesh.clip("x").extract_feature_edges() is_top_bot = np.isin(edges.points[:, 1], [-800, -460]) is_left = edges.points[:, 0] == 0 pts = edges.points[np.invert(is_top_bot | is_left)] sample_4 = pv.PolyData(pts).sample(mesh) .. GENERATED FROM PYTHON SOURCE LINES 128-129 Now we plot all samples for comparison. .. GENERATED FROM PYTHON SOURCE LINES 131-140 .. code-block:: Python fig, axs = plt.subplots(ncols=2, figsize=[26, 10]) u_x = ot.variables.displacement["x"] mesh.plot_contourf(u_x, fig=fig, ax=axs[1]) for i, sample in enumerate([sample_1, sample_2, sample_3, sample_4]): c = f"C{i}" # cycle through default color cycle ot.plot.line(sample, ax=axs[1], linestyle="--", color=c) ot.plot.line(sample, u_x, "y", ax=axs[0], label=f"sample {i + 1}", color=c) fig.tight_layout() .. image-sg:: /auto_examples/howto_postprocessing/images/sphx_glr_plot_sample_mesh_line_004.png :alt: plot sample mesh line :srcset: /auto_examples/howto_postprocessing/images/sphx_glr_plot_sample_mesh_line_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 141-143 If you want to sample data over multiple timesteps in a MeshSeries, have a look at :ref:`sphx_glr_auto_examples_howto_plot_plot_timeslice.py`. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.156 seconds) .. _sphx_glr_download_auto_examples_howto_postprocessing_plot_sample_mesh_line.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_sample_mesh_line.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_sample_mesh_line.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_sample_mesh_line.zip `