| 1 |
""" |
| 2 |
.. _point_labels_example: |
| 3 |
|
| 4 |
Label Points |
| 5 |
~~~~~~~~~~~~ |
| 6 |
|
| 7 |
Use string arrays in a point set to label points |
| 8 |
""" |
| 9 |
|
| 10 |
import numpy as np |
| 11 |
import pyvista as pv |
| 12 |
|
| 13 |
# sphinx_gallery_start_ignore |
| 14 |
# labels are not currently supported by vtk-js |
| 15 |
PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True |
| 16 |
# sphinx_gallery_end_ignore |
| 17 |
|
| 18 |
# sphinx_gallery_thumbnail_number = 3 |
| 19 |
from pyvista import examples |
| 20 |
|
| 21 |
# %% |
| 22 |
# Label String Array |
| 23 |
# ++++++++++++++++++ |
| 24 |
# |
| 25 |
# This example will label the nodes of a mesh with a given array of string |
| 26 |
# labels for each of the nodes. |
| 27 |
|
| 28 |
# Make some random points. Seed the rng for reproducibility. |
| 29 |
rng = np.random.default_rng(seed=0) |
| 30 |
poly = pv.PolyData(rng.random((10, 3))) |
| 31 |
|
| 32 |
# %% |
| 33 |
# Add string labels to the point data - this associates a label with every |
| 34 |
# node: |
| 35 |
|
| 36 |
poly['My Labels'] = [f'Label {i}' for i in range(poly.n_points)] |
| 37 |
poly |
| 38 |
|
| 39 |
# %% |
| 40 |
# Now plot the points with labels using :func:`~pyvista.Plotter.add_point_labels`. |
| 41 |
|
| 42 |
pl = pv.Plotter() |
| 43 |
pl.add_point_labels(poly, 'My Labels', point_size=20, font_size=36) |
| 44 |
pl.show() |
| 45 |
|
| 46 |
|
| 47 |
# %% |
| 48 |
# Label Node Locations |
| 49 |
# ++++++++++++++++++++ |
| 50 |
# |
| 51 |
# This example will label the nodes of a mesh with their coordinate locations |
| 52 |
|
| 53 |
# Load example beam file |
| 54 |
grid = pv.UnstructuredGrid(examples.hexbeamfile) |
| 55 |
|
| 56 |
|
| 57 |
# %% |
| 58 |
# Create plotting class and add the unstructured grid |
| 59 |
pl = pv.Plotter() |
| 60 |
pl.add_mesh(grid, show_edges=True, color='lightblue') |
| 61 |
|
| 62 |
# Add labels to points on the yz plane (where x == 0) |
| 63 |
points = grid.points |
| 64 |
mask = points[:, 0] == 0 |
| 65 |
pl.add_point_labels(points[mask], points[mask].tolist(), point_size=20, font_size=36) |
| 66 |
|
| 67 |
pl.camera_position = pv.CameraPosition( |
| 68 |
position=(-1.5, 1.5, 3.0), focal_point=(0.05, 0.6, 1.2), viewup=(0.2, 0.9, -0.25) |
| 69 |
) |
| 70 |
|
| 71 |
pl.show() |
| 72 |
|
| 73 |
|
| 74 |
# %% |
| 75 |
# Label Scalar Values |
| 76 |
# +++++++++++++++++++ |
| 77 |
# |
| 78 |
# This example will label each point with their scalar values |
| 79 |
|
| 80 |
mesh = examples.load_uniform().slice() |
| 81 |
|
| 82 |
# %% |
| 83 |
pl = pv.Plotter() |
| 84 |
|
| 85 |
# Add the mesh: |
| 86 |
pl.add_mesh(mesh, scalars='Spatial Point Data', show_edges=True) |
| 87 |
# Add the points with scalar labels: |
| 88 |
pl.add_point_scalar_labels(mesh, 'Spatial Point Data', point_size=20, font_size=36) |
| 89 |
|
| 90 |
# Use a nice camera position: |
| 91 |
pl.camera_position = pv.CameraPosition( |
| 92 |
position=(7, 4, 5), focal_point=(4.4, 7.0, 7.2), viewup=(0.8, 0.5, 0.25) |
| 93 |
) |
| 94 |
|
| 95 |
pl.show() |
| 96 |
# %% |
| 97 |
# .. tags:: plot |