PyVista 字形中数组的真值不明确,东方无法识别我的向量数组

问题描述 投票:0回答:1

我正在尝试将自旋向量作为箭头添加到晶格的顶点上。然而我总是得到:

第 2382 行,字形 如果定向: ^^^^^^ ValueError:具有多个元素的数组的真值不明确。使用 a.any() 或 a.all()

这是我第一次使用 PyVista,我不知道。

import numpy as np
import vtk
import pyvista as pv

cols = 12
rows = 12
spacing = 10.0


points = []

# create lattice
for i in range(rows):
    for j in range(cols):
        x = j * spacing
        y = i * (spacing * np.sqrt(3) / 2)
        if i % 2 == 1:  
            x += spacing / 2
        points.append([x, y, 0.0])

points = np.array(points)  
spins = np.random.choice([-1, 1], size=(len(points), 3))


normed_spins = spins / np.linalg.norm(spins, axis=1)[:, np.newaxis]


lattice = pv.PolyData(points)

arrows = lattice.glyph(orient=normed_spins, scale=True, factor=0.5)
print(spins[:5])

plotter = pv.Plotter()


plotter.add_mesh(lattice, color="black", point_size=10, render_points_as_spheres=True, label="Gitterpunkte")


plotter.add_mesh(arrows, color="red", label="Spins")


plotter.show_bounds(grid="front", location="outer", all_edges=True)
plotter.show(title=)

python plot physics pyvista
1个回答
0
投票

您使用

glyph
不正确

glyph(orient=True, scale=True, factor=1.0, geom=None, indices=None, tolerance=None, absolute=False, clamping=False, rng=None, color_mode='scale', progress_bar=False) method of pyvista.core.pointset.PolyData instance
    Copy a geometric representation (called a glyph) to the input dataset.

    The glyph may be oriented along the input vectors, and it may
    be scaled according to scalar data or vector
    magnitude. Passing a table of glyphs to choose from based on
    scalars or vector magnitudes is also supported.  The arrays
    used for ``orient`` and ``scale`` must be either both point data
    or both cell data.

    Parameters
    ----------
    orient : bool | str, default: True
        If ``True``, use the active vectors array to orient the glyphs.
        If string, the vector array to use to orient the glyphs.
        If ``False``, the glyphs will not be orientated.
    enter code here
...

注意 orient 可以是字符串也可以是布尔值。您传递了错误的数据类型。您将传递一个 numpy 数组 (normed_spins) 作为lattice.glyph() 函数中的 orient 参数的值。 PyVista 的 glyph() 方法期望 orient 参数是 PolyData 对象中存在的向量数组的名称,而不是数组本身。

来源:https://tutorial.pyvista.org/tutorial/04_filters/solutions/e_glyphs.html

© www.soinside.com 2019 - 2024. All rights reserved.