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