使用PyVista可视化3D数据时遇UniformGrid属性错误及值相关问题求助
PyVista可视化3D数据的AttributeError问题解决
问题描述
运行以下PyVista可视化代码时出现AttributeError: module 'pyvista' has no attribute 'UniformGrid'错误(使用最新版PyVista):
from openpmd_viewer import OpenPMDTimeSeries import pyvista # Open the simulation outputs using openPMD viewer ts = OpenPMDTimeSeries('./sim_outputs/diags/hdf5') # Create the PyVista plotter plotter = pyvista.Plotter() plotter.set_background("white") # Retrieve the rho field from the simulation # The theta=None argument constructs a 3D cartesian grid from the cylindrical data rho, meta = ts.get_field("rho", iteration=ts.iterations[-1], theta=None) # Create the grid on which PyVista can deposit the data grid = pyvista.UniformGrid() grid.dimensions = rho.shape grid.origin = [meta.xmin * 1e6, meta.ymin * 1e6, meta.zmin * 1e6] grid.spacing = [meta.dx * 1e6, meta.dy * 1e6, meta.dz * 1e6] grid.point_data['values'] = -rho.flatten(order='F') # Add the grid to the plotter # Use a cutoff for rho via the clim argument since otherwise it shows only a small density spike plotter.add_volume(grid, clim=(0, 4e6), opacity='sigmoid', cmap='viridis', mapper='gpu', show_scalar_bar=False) # A good starting camera position - the three values are the camera position, # the camera focus, and the up vector of the viewport plotter.camera_position = [(-74, 32, 51), (0, 0, 88), (0, 1, 0)] plotter.show()
将UniformGrid替换为StructuredGrid后,又出现与values相关的维度不匹配错误。
错误原因
- UniformGrid报错:最新版PyVista中
UniformGrid类是存在的,报错大概率是导入路径冲突(如环境中存在同名pyvista模块)、PyVista版本实际未更新到位,或者导入方式问题。 - StructuredGrid的values错误:
StructuredGrid与UniformGrid的初始化逻辑不同,前者需要显式生成网格点坐标,不能直接通过origin和spacing设置,若跳过这一步会导致点数据的维度不匹配。
解决方案
方案1:修复UniformGrid的使用
先确认PyVista版本,再调整导入方式:
from openpmd_viewer import OpenPMDTimeSeries # 直接导入UniformGrid,避免模块属性查找问题 from pyvista import Plotter, UniformGrid ts = OpenPMDTimeSeries('./sim_outputs/diags/hdf5') plotter = Plotter() plotter.set_background("white") rho, meta = ts.get_field("rho", iteration=ts.iterations[-1], theta=None) # 正确初始化UniformGrid grid = UniformGrid() grid.dimensions = rho.shape grid.origin = [meta.xmin * 1e6, meta.ymin * 1e6, meta.zmin * 1e6] grid.spacing = [meta.dx * 1e6, meta.dy * 1e6, meta.dz * 1e6] grid.point_data['values'] = -rho.flatten(order='F') plotter.add_volume(grid, clim=(0, 4e6), opacity='sigmoid', cmap='viridis', mapper='gpu', show_scalar_bar=False) plotter.camera_position = [(-74, 32, 51), (0, 0, 88), (0, 1, 0)] plotter.show()
若仍报错,执行pip install --upgrade pyvista确保版本为最新,同时检查环境中是否有其他同名模块干扰。
方案2:正确使用StructuredGrid
若需使用StructuredGrid,需先生成网格点坐标:
from openpmd_viewer import OpenPMDTimeSeries import pyvista import numpy as np ts = OpenPMDTimeSeries('./sim_outputs/diags/hdf5') plotter = pyvista.Plotter() plotter.set_background("white") rho, meta = ts.get_field("rho", iteration=ts.iterations[-1], theta=None) # 生成坐标轴数组 x = np.linspace(meta.xmin * 1e6, meta.xmax * 1e6, rho.shape[0]) y = np.linspace(meta.ymin * 1e6, meta.ymax * 1e6, rho.shape[1]) z = np.linspace(meta.zmin * 1e6, meta.zmax * 1e6, rho.shape[2]) # 创建结构化网格的点坐标(保持与rho的维度顺序一致) xx, yy, zz = np.meshgrid(x, y, z, indexing='ij') points = np.column_stack((xx.ravel(order='F'), yy.ravel(order='F'), zz.ravel(order='F'))) # 初始化StructuredGrid并设置属性 grid = pyvista.StructuredGrid() grid.points = points grid.dimensions = rho.shape grid.point_data['values'] = -rho.flatten(order='F') plotter.add_volume(grid, clim=(0, 4e6), opacity='sigmoid', cmap='viridis', mapper='gpu', show_scalar_bar=False) plotter.camera_position = [(-74, 32, 51), (0, 0, 88), (0, 1, 0)] plotter.show()
这里通过numpy.meshgrid生成与rho维度匹配的点坐标,确保point_data['values']的长度与点数量一致,避免维度错误。
内容的提问来源于stack exchange,提问作者Dinkar Mishra
相关产品推荐
相关产品推荐

