.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/plot_02_weighted_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_02_weighted_sampling.py: .. _weighted_random_surface_sampling_example: Weighted Random Surface Sampling ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Sampling can be weighted on the mesh. Compare to :ref:`random_surface_sampling_example`. .. GENERATED FROM PYTHON SOURCE LINES 9-17 .. code-block:: Python import pyvista as pv from pyvista import examples import pyransame antarctica = examples.download_antarctica_velocity() .. GENERATED FROM PYTHON SOURCE LINES 18-20 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 20-23 .. code-block:: Python antarctica.points /= 1000.0 # convert to kilometers .. GENERATED FROM PYTHON SOURCE LINES 24-29 The random sampling occurs over the area of the mesh, i.e. inside the cells. So cell data is needed for weighting. Here, ``pyvista.cell_centers`` is used to get position of the cells relative to the top of the mesh. ``pyvista.DataSetFilters.point_data_to_cell_data`` filter can be used to convert point data to the needed cell data if required. .. GENERATED FROM PYTHON SOURCE LINES 29-33 .. code-block:: Python ymax = antarctica.bounds[3] weights = (ymax - antarctica.cell_centers().points[:, 1]) ** 2 .. GENERATED FROM PYTHON SOURCE LINES 34-35 Do weighted sampling. .. GENERATED FROM PYTHON SOURCE LINES 35-38 .. code-block:: Python points = pyransame.random_surface_dataset(antarctica, 500, weights=weights) .. GENERATED FROM PYTHON SOURCE LINES 39-40 Now plot result. .. GENERATED FROM PYTHON SOURCE LINES 40-48 .. code-block:: Python pl = pv.Plotter() pl.add_mesh(antarctica, color="tan") spheres = pv.wrap(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_02_weighted_sampling_001.png :alt: plot 02 weighted sampling :srcset: /examples/images/sphx_glr_plot_02_weighted_sampling_001.png :class: sphx-glr-single-img .. tab-item:: Interactive Scene .. offlineviewer:: /home/runner/work/pyransame/pyransame/doc/examples/images/sphx_glr_plot_02_weighted_sampling_001.vtksz .. GENERATED FROM PYTHON SOURCE LINES 49-50 The same thing can be done with cell data on the mesh. .. GENERATED FROM PYTHON SOURCE LINES 50-54 .. code-block:: Python antarctica.cell_data["weights"] = weights points = pyransame.random_surface_dataset(antarctica, 500, weights="weights") .. GENERATED FROM PYTHON SOURCE LINES 55-57 Now plot result. The result will be slightly different due to random nature of the sampling. .. GENERATED FROM PYTHON SOURCE LINES 57-64 .. code-block:: Python pl = pv.Plotter() pl.add_mesh(antarctica, color="tan") spheres = pv.wrap(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_02_weighted_sampling_002.png :alt: plot 02 weighted sampling :srcset: /examples/images/sphx_glr_plot_02_weighted_sampling_002.png :class: sphx-glr-single-img .. tab-item:: Interactive Scene .. offlineviewer:: /home/runner/work/pyransame/pyransame/doc/examples/images/sphx_glr_plot_02_weighted_sampling_002.vtksz .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 17.374 seconds) .. _sphx_glr_download_examples_plot_02_weighted_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_02_weighted_sampling.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_weighted_sampling.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_