Debian Codegraph @

examples/02-plot/color_lines.py

raw
1 """
2 .. _color_lines_example:
3
4 Color Several Lines
5 ~~~~~~~~~~~~~~~~~~~
6
7 Render several :func:`pyvista.MultipleLines` polylines and color them by a
8 line-wise scalar value.
9 """
10
11 import numpy as np
12 import pyvista as pv
13
14 # %%
15 # Build a small family of curves
16 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17 # Each curve carries a single ``line_id`` cell scalar so the merged dataset
18 # can be colored line-by-line.
19
20 lines = []
21 for index, phase in enumerate(np.linspace(0, np.pi, 4)):
22 t = np.linspace(0, 1, 80)
23 points = np.column_stack(
24 (
25 4 * t - 2,
26 np.sin(2 * np.pi * t + phase),
27 0.4 * np.cos(np.pi * t + phase),
28 ),
29 )
30 line = pv.MultipleLines(points)
31 line.cell_data['line_id'] = np.array([index])
32 lines.append(line)
33
34 curves = pv.merge(lines)
35 curves
36
37
38 # %%
39 # Tube and color the lines
40 # ~~~~~~~~~~~~~~~~~~~~~~~~
41 # Coloring by ``line_id`` gives each line a single uniform color.
42
43 pl = pv.Plotter()
44 pl.add_mesh(
45 curves.tube(radius=0.06),
46 scalars='line_id',
47 cmap='viridis',
48 show_scalar_bar=False,
49 )
50 pl.show()
51 # %%
52 # .. tags:: plot