| 1 |
""" |
| 2 |
.. _point_clouds_example: |
| 3 |
|
| 4 |
Plotting Point Clouds |
| 5 |
~~~~~~~~~~~~~~~~~~~~~ |
| 6 |
This example shows you how to plot point clouds using PyVista using both the |
| 7 |
``'points'`` and ``'points_gaussian'`` styles. |
| 8 |
|
| 9 |
""" |
| 10 |
|
| 11 |
import numpy as np |
| 12 |
import pyvista as pv |
| 13 |
from pyvista import examples |
| 14 |
|
| 15 |
# sphinx_gallery_start_ignore |
| 16 |
# point gaussian does not work in interactive plots |
| 17 |
PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True |
| 18 |
# sphinx_gallery_end_ignore |
| 19 |
|
| 20 |
# %% |
| 21 |
# Compare the Plotting methods |
| 22 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 23 |
# First, let's create a sample point cloud using :func:`numpy.random.random`. |
| 24 |
|
| 25 |
# Seed rng for reproducibility |
| 26 |
rng = np.random.default_rng(seed=0) |
| 27 |
points = rng.random((1000, 3)) |
| 28 |
points |
| 29 |
|
| 30 |
# %% |
| 31 |
# Basic Plot |
| 32 |
# ~~~~~~~~~~ |
| 33 |
# We can simply plot this point cloud using the convenience :func:`pyvista.plot` |
| 34 |
# function. |
| 35 |
|
| 36 |
pv.plot(points) |
| 37 |
|
| 38 |
|
| 39 |
# %% |
| 40 |
# Plot with Scalars |
| 41 |
# ~~~~~~~~~~~~~~~~~ |
| 42 |
# That's quite boring, so let's spice things up by adding color. We can either |
| 43 |
# use a single scalar to plot the points. For example, the z coordinates. |
| 44 |
# |
| 45 |
# For fun, let's also render the points as spheres. |
| 46 |
pv.plot( |
| 47 |
points, |
| 48 |
scalars=points[:, 2], |
| 49 |
render_points_as_spheres=True, |
| 50 |
point_size=20, |
| 51 |
show_scalar_bar=False, |
| 52 |
) |
| 53 |
|
| 54 |
|
| 55 |
# %% |
| 56 |
# Plot with RGBA |
| 57 |
# ~~~~~~~~~~~~~~ |
| 58 |
# Alternatively, we can color the point cloud using an RGBA array. This has |
| 59 |
# been normalized from (0, 1), but we could have also used a ``numpy.uint8`` |
| 60 |
# array from 0-255. |
| 61 |
rgba = points - points.min(axis=0) |
| 62 |
rgba /= rgba.max(axis=0) |
| 63 |
pv.plot( |
| 64 |
points, |
| 65 |
scalars=rgba, |
| 66 |
render_points_as_spheres=True, |
| 67 |
point_size=20, |
| 68 |
cpos='xy', |
| 69 |
rgba=True, |
| 70 |
) |
| 71 |
|
| 72 |
# %% |
| 73 |
# Point Cloud Plot Styles |
| 74 |
# ~~~~~~~~~~~~~~~~~~~~~~~ |
| 75 |
# PyVista supports the ``'points_gaussian'`` style, which renders points as |
| 76 |
# individual soft sprites. You have the option of displaying these as tight |
| 77 |
# "spheres" using ``render_points_as_spheres=True`` (default), or disabling it |
| 78 |
# to create softer points at the expense of render performance. |
| 79 |
# |
| 80 |
# Here's the basic plot again, but with the style as ``'points_gaussian'``: |
| 81 |
pv.plot(points, style='points_gaussian', opacity=0.5, point_size=15) |
| 82 |
|
| 83 |
|
| 84 |
# %% |
| 85 |
# Here's a plotter with four combinations of the options side-by-side so you |
| 86 |
# can see for yourself the different options available when plotting these |
| 87 |
# points. PyVista tries to achieve sensible defaults, but should you find these |
| 88 |
# insufficient for your needs, feel free to play around with the various options |
| 89 |
# and find something that works for you. |
| 90 |
|
| 91 |
pl = pv.Plotter(shape=(2, 2)) |
| 92 |
|
| 93 |
# Standard points |
| 94 |
actor = pl.add_points( |
| 95 |
points, |
| 96 |
style='points', |
| 97 |
emissive=False, |
| 98 |
scalars=rgba, |
| 99 |
rgba=True, |
| 100 |
point_size=10, |
| 101 |
ambient=0.7, |
| 102 |
) |
| 103 |
pl.add_text('"points" not as spheres', color='w') |
| 104 |
|
| 105 |
# Gaussian points |
| 106 |
pl.subplot(0, 1) |
| 107 |
actor = pl.add_points( |
| 108 |
points, |
| 109 |
render_points_as_spheres=False, |
| 110 |
style='points_gaussian', |
| 111 |
emissive=False, |
| 112 |
scalars=rgba, |
| 113 |
rgba=True, |
| 114 |
opacity=0.99, |
| 115 |
point_size=10, |
| 116 |
ambient=1.0, |
| 117 |
) |
| 118 |
pl.add_text('"points_gaussian" not as spheres\nemissive=False', color='w') |
| 119 |
|
| 120 |
# Gaussian points with emissive=True |
| 121 |
pl.subplot(1, 0) |
| 122 |
actor = pl.add_points( |
| 123 |
points, |
| 124 |
render_points_as_spheres=False, |
| 125 |
style='points_gaussian', |
| 126 |
emissive=True, |
| 127 |
scalars=rgba, |
| 128 |
rgba=True, |
| 129 |
point_size=10, |
| 130 |
) |
| 131 |
pl.add_text('"points_gaussian" not as spheres\nemissive=True', color='w') |
| 132 |
|
| 133 |
# With render_points_as_spheres=True |
| 134 |
pl.subplot(1, 1) |
| 135 |
actor = pl.add_points( |
| 136 |
points, |
| 137 |
style='points_gaussian', |
| 138 |
render_points_as_spheres=True, |
| 139 |
scalars=rgba, |
| 140 |
rgba=True, |
| 141 |
point_size=10, |
| 142 |
) |
| 143 |
pl.add_text('"points_gaussian" as spheres', color='w') |
| 144 |
|
| 145 |
pl.background_color = 'k' |
| 146 |
pl.link_views() |
| 147 |
pl.camera_position = 'xy' |
| 148 |
pl.camera.zoom(1.2) |
| 149 |
pl.show() |
| 150 |
|
| 151 |
|
| 152 |
# %% |
| 153 |
# Orbit a Point Cloud |
| 154 |
# ~~~~~~~~~~~~~~~~~~~ |
| 155 |
# Generate a plot orbiting around a point cloud. Color based on the distance |
| 156 |
# from the center of the cloud using :func:`~pyvista.Plotter.generate_orbital_path`. |
| 157 |
|
| 158 |
cloud = examples.download_cloud_dark_matter() |
| 159 |
scalars = np.linalg.norm(cloud.points - cloud.center, axis=1) |
| 160 |
|
| 161 |
pl = pv.Plotter(off_screen=True) |
| 162 |
pl.add_mesh( |
| 163 |
cloud, |
| 164 |
style='points_gaussian', |
| 165 |
color='#fff7c2', |
| 166 |
scalars=scalars, |
| 167 |
opacity=0.25, |
| 168 |
point_size=4.0, |
| 169 |
show_scalar_bar=False, |
| 170 |
) |
| 171 |
pl.background_color = 'k' |
| 172 |
pl.show(auto_close=False) |
| 173 |
path = pl.generate_orbital_path(n_points=36, shift=cloud.length, factor=3.0) |
| 174 |
pl.open_gif('orbit_cloud.gif') |
| 175 |
pl.orbit_on_path(path, write_frames=True) |
| 176 |
pl.close() |
| 177 |
# %% |
| 178 |
# .. tags:: plot |