グリフ(ベクトルまたはポリデータ)のプロット#

データセット内のベクトルを使用して、グリフ/幾何学的オブジェクトのプロットと方向付けを行います。

import numpy as np

# sphinx_gallery_thumbnail_number = 4
import pyvista as pv
from pyvista import examples

グリフ化は、pyvista.DataSetFilters.glyph()フィルタを使用して実行できます。

mesh = examples.download_carotid().threshold(145, scalars="scalars")
mask = mesh["scalars"] < 210
mesh["scalars"][mask] = 0  # null out smaller vectors

# Make a geometric object to use as the glyph
geom = pv.Arrow()  # This could be any dataset

# Perform the glyph
glyphs = mesh.glyph(orient="vectors", scale="scalars", factor=0.003, geom=geom)

# plot using the plotting class
pl = pv.Plotter()
pl.add_mesh(glyphs, show_scalar_bar=False, lighting=False, cmap="coolwarm")
pl.camera_position = [
    (146.53, 91.28, 21.70),
    (125.00, 94.45, 19.81),
    (-0.086, 0.007, 0.996),
]  # view only part of the vector field
cpos = pl.show(return_cpos=True)
plot glyphs

もう1つのアプローチとしては、ベクトルをメッシュオブジェクトに直接読み込み、pyvista.DataSet.arrowsプロパティにアクセスする方法があります。

sphere = pv.Sphere(radius=3.14)

# make cool swirly pattern
vectors = np.vstack(
    (
        np.sin(sphere.points[:, 0]),
        np.cos(sphere.points[:, 1]),
        np.cos(sphere.points[:, 2]),
    )
).T

# add and scale
sphere["vectors"] = vectors * 0.3
sphere.set_active_vectors("vectors")

# plot just the arrows
sphere.arrows.plot()
plot glyphs

矢印と球体をプロットします。

p = pv.Plotter()
p.add_mesh(sphere.arrows, lighting=False, scalar_bar_args={"title": "Vector Magnitude"})
p.add_mesh(sphere, color="grey", ambient=0.6, opacity=0.5, show_edges=False)
p.show()
plot glyphs

グリフのサブセット#

入力データセットのすべてのノードにグリフを配置したくない場合があります。この場合、マージ許容値を使用して、入力データセットのサブセットにグリフを作成することを選択できます。ここでは、バウンディングボックスの長さの5パーセントに相当する、5パーセントのマージ許容値を指定します。

# Example dataset with normals
mesh = examples.load_random_hills()

# create a subset of arrows using the glyph filter
arrows = mesh.glyph(scale="Normals", orient="Normals", tolerance=0.05)

p = pv.Plotter()
p.add_mesh(arrows, color="black")
p.add_mesh(mesh, scalars="Elevation", cmap="terrain", smooth_shading=True)
p.show()
plot glyphs

スクリプトの実行時間合計:(0分5.249秒)

推定メモリ使用量:524 MB

Sphinx-Galleryによって生成されたギャラリー