.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/plot_01_random_surface_sampling.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_plot_01_random_surface_sampling.py: .. _random_surface_sampling_example: Random Surface Sampling ~~~~~~~~~~~~~~~~~~~~~~~ Random points on a dataset of antarctica are sampled uniformly within the area of the mesh. .. GENERATED FROM PYTHON SOURCE LINES 10-19 .. code-block:: Python import numpy as np import pyvista as pv from pyvista import examples import pyransame antarctica = examples.download_antarctica_velocity() .. GENERATED FROM PYTHON SOURCE LINES 20-22 The units of this mesh are in meters, which causes plotting issues over an entire continent. So the units are first converted to kilometers. .. GENERATED FROM PYTHON SOURCE LINES 22-25 .. code-block:: Python antarctica.points /= 1000.0 # convert to kilometers .. GENERATED FROM PYTHON SOURCE LINES 26-27 sample 500 points uniformly randomly. .. GENERATED FROM PYTHON SOURCE LINES 27-31 .. code-block:: Python points = pyransame.random_surface_dataset(antarctica, 500) points .. raw:: html
HeaderData Arrays
PolyDataInformation
N Cells500
N Points500
N Strips0
X Bounds-2.453e+03, 2.463e+03
Y Bounds-2.126e+03, 2.157e+03
Z Bounds0.000e+00, 0.000e+00
N Arrays5
NameFieldTypeN CompMinMax
node_valuePointsint6410.000e+002.300e+01
ssavelocityPointsfloat643-8.487e+021.208e+03
vtkValidPointMaskPointsint811.000e+001.000e+00
vtkGhostTypePointsuint810.000e+000.000e+00
vtkGhostTypeCellsuint810.000e+000.000e+00


.. GENERATED FROM PYTHON SOURCE LINES 32-36 :func:`pyransame.random_surface_dataset` returns a `pyvista.PolyData` object containing 500 points (and 500 corresponding vertex cells). The cell and point data is also sampled onto ``points``. Each randomly sampled point is plotted as a sphere with radius 50 km and colored by ``ssavelocity``. .. GENERATED FROM PYTHON SOURCE LINES 36-44 .. code-block:: Python pl = pv.Plotter() pl.add_mesh(antarctica, color="tan") spheres = points.glyph(geom=pv.Sphere(radius=50), scale=False, orient=False) pl.add_mesh(spheres, scalars="ssavelocity", clim=[0, 750]) pl.view_xy() pl.show() .. tab-set:: .. tab-item:: Static Scene .. image-sg:: /examples/images/sphx_glr_plot_01_random_surface_sampling_001.png :alt: plot 01 random surface sampling :srcset: /examples/images/sphx_glr_plot_01_random_surface_sampling_001.png :class: sphx-glr-single-img .. tab-item:: Interactive Scene .. offlineviewer:: /home/runner/work/pyransame/pyransame/doc/examples/images/sphx_glr_plot_01_random_surface_sampling_001.vtksz .. GENERATED FROM PYTHON SOURCE LINES 45-52 Comparison to sampling mesh points ---------------------------------- A different approach to sampling is to select random mesh points that define the topology of the mesh. This approach does not lead to effective uniform sampling of mesh area (or length/volume as applicable), which will be demonstrated in this example. The area of each cell is stored first, which will be useful for explaining the later results. .. GENERATED FROM PYTHON SOURCE LINES 52-55 .. code-block:: Python antarctica = antarctica.compute_cell_sizes() .. GENERATED FROM PYTHON SOURCE LINES 56-57 Sample 500 random vertices that define the cells. .. GENERATED FROM PYTHON SOURCE LINES 57-60 .. code-block:: Python vertices = np.random.default_rng().choice(antarctica.points, 500) .. GENERATED FROM PYTHON SOURCE LINES 61-64 Plot the mesh colored by area of each cell. Plot the location of each point. Note that these points do not have sampled data in this case, so color them red. .. GENERATED FROM PYTHON SOURCE LINES 64-72 .. code-block:: Python pl = pv.Plotter() pl.add_mesh(antarctica, scalars="Area") spheres = pv.wrap(vertices).glyph(geom=pv.Sphere(radius=50), scale=False, orient=False) pl.add_mesh(spheres, color="red") pl.view_xy() pl.show() .. tab-set:: .. tab-item:: Static Scene .. image-sg:: /examples/images/sphx_glr_plot_01_random_surface_sampling_002.png :alt: plot 01 random surface sampling :srcset: /examples/images/sphx_glr_plot_01_random_surface_sampling_002.png :class: sphx-glr-single-img .. tab-item:: Interactive Scene .. offlineviewer:: /home/runner/work/pyransame/pyransame/doc/examples/images/sphx_glr_plot_01_random_surface_sampling_002.vtksz .. GENERATED FROM PYTHON SOURCE LINES 73-78 The sampled points are not uniformly sampled on the area of the mesh. The points are clustered in regions that have small cell sizes, i.e. there is a higher density of mesh points that define the cells. ``pyransame`` does not use this approach and instead samples the space uniformly randomly with respect to the area of the mesh. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 19.782 seconds) .. _sphx_glr_download_examples_plot_01_random_surface_sampling.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_random_surface_sampling.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_random_surface_sampling.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_