# Examples from pyvista.DataSetFilters.split_values
# =================================================

# Load image with labeled regions.
import numpy as np
import pyvista as pv
from pyvista import examples
image = examples.load_channels()
np.unique(image.active_scalars)

# Split the image into its separate regions. Here, we also remove the first
# region for visualization.
multiblock = image.split_values()
_ = multiblock.pop(0)  # Remove first region

# Plot the regions.
pl = pv.Plotter()
_ = pl.add_composite(multiblock, multi_colors=True)
_ = pl.show_grid()
pl.show()

# Note that the block names are generic by default.
multiblock.keys()

# To name the output blocks, use a dictionary as input instead.
# Here, we also explicitly omit the region with `0` values from the input
# instead of removing it from the output.
labels = dict(region1=1, region2=2, region3=3, region4=4)

multiblock = image.split_values(labels)
multiblock.keys()

# Plot the regions as separate meshes using the labels instead of plotting
# the MultiBlock directly.
# Clear scalar data so we can color each mesh using a single color
_ = [block.clear_data() for block in multiblock]

pl = pv.Plotter()
pl.set_color_cycler('default')
_ = [
    pl.add_mesh(block, label=label)
    for block, label in zip(multiblock, labels)
]
_ = pl.add_legend()
pl.show()

# ----------------------------------------------------------------------
# Generated by sphinx-examples-as-code https://github.com/pyvista/sphinx-examples-as-code
