.. 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 = antarctica.ransame.surface_dataset(500) points .. raw:: html
PolyData (0x7ff75012e560)
      N Cells:    500
      N Points:   500
      N Strips:   0
      X Bounds:   -2.422e+03, 2.589e+03
      Y Bounds:   -1.989e+03, 2.220e+03
      Z Bounds:   0.000e+00, 0.000e+00
      N Arrays:   5


.. GENERATED FROM PYTHON SOURCE LINES 32-38 The ``ransame`` accessor on the dataset (registered by ``pyransame``) forwards to :func:`pyransame.random_surface_dataset`, which 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 38-46 .. 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() .. 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 .. GENERATED FROM PYTHON SOURCE LINES 47-54 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 54-57 .. code-block:: Python antarctica = antarctica.compute_cell_sizes() .. GENERATED FROM PYTHON SOURCE LINES 58-59 Sample 500 random vertices that define the cells. .. GENERATED FROM PYTHON SOURCE LINES 59-62 .. code-block:: Python vertices = np.random.default_rng().choice(antarctica.points, 500) .. GENERATED FROM PYTHON SOURCE LINES 63-66 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 66-74 .. 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() .. 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 .. GENERATED FROM PYTHON SOURCE LINES 75-80 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 6.214 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 ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_random_surface_sampling.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_