| 1 |
""" |
| 2 |
.. _points_gaussian_scale_example: |
| 3 |
|
| 4 |
Scaled Gaussian Points |
| 5 |
---------------------- |
| 6 |
This example demonstrates how to plot spheres using the ``'points_gaussian'`` |
| 7 |
style with :func:`~pyvista.Plotter.add_mesh` and scale them by a dynamic radius. |
| 8 |
|
| 9 |
""" |
| 10 |
|
| 11 |
import numpy as np |
| 12 |
import pyvista as pv |
| 13 |
|
| 14 |
# sphinx_gallery_start_ignore |
| 15 |
# gaussian does not work in VTK.js |
| 16 |
PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True |
| 17 |
# sphinx_gallery_end_ignore |
| 18 |
|
| 19 |
# %% |
| 20 |
# First, generate the sphere positions and radii randomly on the edge of a |
| 21 |
# torus. |
| 22 |
|
| 23 |
# Seed the rng for reproducibility |
| 24 |
rng = np.random.default_rng(seed=0) |
| 25 |
|
| 26 |
N_SPHERES = 10_000 |
| 27 |
theta = rng.uniform(0, 2 * np.pi, N_SPHERES) |
| 28 |
phi = rng.uniform(0, 2 * np.pi, N_SPHERES) |
| 29 |
torus_radius = 1 |
| 30 |
tube_radius = 0.3 |
| 31 |
radius = torus_radius + tube_radius * np.cos(phi) |
| 32 |
rad = rng.random(N_SPHERES) * 0.01 |
| 33 |
|
| 34 |
pos = np.zeros((N_SPHERES, 3)) |
| 35 |
pos[:, 0] = radius * np.cos(theta) |
| 36 |
pos[:, 1] = radius * np.sin(theta) |
| 37 |
pos[:, 2] = tube_radius * np.sin(phi) |
| 38 |
|
| 39 |
|
| 40 |
# %% |
| 41 |
# Next, create a PolyData object and add the sphere positions and radii as |
| 42 |
# data arrays. |
| 43 |
|
| 44 |
pdata = pv.PolyData(pos) |
| 45 |
pdata['radius'] = rad |
| 46 |
|
| 47 |
|
| 48 |
# %% |
| 49 |
# Finally, plot the spheres using the ``points_gaussian`` style and scale them |
| 50 |
# by radius. |
| 51 |
|
| 52 |
pl = pv.Plotter() |
| 53 |
actor = pl.add_mesh( |
| 54 |
pdata, |
| 55 |
style='points_gaussian', |
| 56 |
emissive=False, |
| 57 |
render_points_as_spheres=True, |
| 58 |
show_scalar_bar=False, |
| 59 |
) |
| 60 |
actor.mapper.scale_array = 'radius' |
| 61 |
pl.camera.zoom(1.5) |
| 62 |
pl.show() |
| 63 |
|
| 64 |
|
| 65 |
# %% |
| 66 |
# Show the same plot with ``emissive=True``. |
| 67 |
|
| 68 |
pl = pv.Plotter() |
| 69 |
pl.background_color = 'k' |
| 70 |
actor = pl.add_mesh( |
| 71 |
pdata, |
| 72 |
style='points_gaussian', |
| 73 |
emissive=True, |
| 74 |
render_points_as_spheres=True, |
| 75 |
show_scalar_bar=False, |
| 76 |
) |
| 77 |
actor.mapper.scale_array = 'radius' |
| 78 |
pl.camera.zoom(1.5) |
| 79 |
pl.show() |
| 80 |
# %% |
| 81 |
# .. tags:: plot |