pyvista 3D Visualization Issue

689 Views Asked by At

I am trying to visualize 3D data using pyvista. Initially the code given by the developer of the software was

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()

But it gives me error and says AttributeError: module 'pyvista' has no attribute 'UniformGrid' I am using latest version of pyvista at present. I changed the uniform grid to structured grid but still it gives me error about values

2

There are 2 best solutions below

0
Matthew Flamm On

We cannot run your code, so please give a reproducible example. On the newer versions of PyVista, UniformGrid was renamed to ImageData.

0
Musabbir Arrafi On

pyvista.UniformGrid() has been deprecated from pyvista=0.42 and removed in pyvista=0.43.1 which is most current version I assume you're using. Try to downgrade to pyvista=0.42 with the following:

pip install pyvista==0.42.3

Here's a example usage from pyvista=0.42.3:

import pyvista as pv
import numpy as np

# Create a uniform grid
grid = pv.UniformGrid()

# Set grid dimensions and spacing
grid.dimensions = [10, 10, 10]  # Number of points along each axis
grid.spacing = [1, 1, 1]  # Spacing between points along each axis

# Create some scalar values (you can modify this based on your data)
scalars = np.arange(grid.n_points)

# Add the scalar values to the grid
grid["Scalars"] = scalars  # This sets scalars as a point data array

# Plot the uniform grid
plotter = pv.Plotter()
plotter.add_mesh(grid, scalars="Scalars", cmap="viridis")
plotter.show()

Output: pyvista-output