Color Several Lines#

Render several pyvista.MultipleLines() polylines and color them by a line-wise scalar value.

import numpy as np
import pyvista as pv

Build a small family of curves#

Each curve carries a single line_id cell scalar so the merged dataset can be colored line-by-line.

lines = []
for index, phase in enumerate(np.linspace(0, np.pi, 4)):
    t = np.linspace(0, 1, 80)
    points = np.column_stack(
        (
            4 * t - 2,
            np.sin(2 * np.pi * t + phase),
            0.4 * np.cos(np.pi * t + phase),
        ),
    )
    line = pv.MultipleLines(points)
    line.cell_data['line_id'] = np.array([index])
    lines.append(line)

curves = pv.merge(lines)
curves
PolyData (0x7ea3d19bb280)
  N Cells:    4
  N Points:   320
  N Strips:   0
  X Bounds:   -2.000e+00, 2.000e+00
  Y Bounds:   -1.000e+00, 9.998e-01
  Z Bounds:   -4.000e-01, 4.000e-01
  N Arrays:   2


Tube and color the lines#

Coloring by line_id gives each line a single uniform color.

pl = pv.Plotter()
pl.add_mesh(
    curves.tube(radius=0.06),
    scalars='line_id',
    cmap='viridis',
    show_scalar_bar=False,
)
pl.show()
color lines

Tags: plot

Total running time of the script: (0 minutes 0.123 seconds)

Gallery generated by Sphinx-Gallery