Open In App

Creating Interactive 3D Visualizations using PyVista

Last Updated : 01 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

In this article, we will first explore the benefits of using PyVista in Python. Next, we will guide you through the installation of this library on your system. Then, we will demonstrate how to create various 3D visualizations such as sphere visualizations, structured grid visualizations, point cloud visualizations, and more.

What is PyVista in Python?

PyVista is a powerful Python library designed for creating interactive 3D visualizations. Built on Numpy arrays, it offers a straightforward approach to constructing 3D plots. With its high-level API for mesh analysis, PyVista proves to be an essential and user-friendly tool for data scientists and analysts alike. It is well-suited for scientific plotting in presentations or research papers. Additionally, PyVista serves as a supporting library for other 3D mesh rendering tools.

Benefits of PyVista in Python

  • We can also use PyVista in Jupyter Notebooks, leveraging server-side and client-side rendering through Trame.
  • PyVista provides features such as filtering and plotting tools for a more interactive experience.
  • This library provides direct access to routines for mesh analysis and transformation.
  • PyVista has a syntax similar to matplotlib.
  • We can export meshes in various formats, such as VTK, STL, OBJ, and PLY.
  • These formats are handled by Meshio and allow the import of meshes from any of the formats mentioned above.

Installation

To install PyVista into your system, use the below command in your terminal.

To install PyVista in a notebook

pip install pyvista

To install PyVista from GitHub,

python3 setup.py install

Examples to Create Interactive 3D Visualizations using PyVista

Sphere Visualization

In this example, we will create a 3D sphere object using PyVista. We can adjust the color and edges of the sphere using different parameters. Using this plot, we can gain different perspectives on the data. We can interact with the sphere by zooming in and by rotating it. This plot is a great starting point for understanding how to create plots using PyVista.

To create this simple 3D sphere visualization, we will first start by importing the PyVista library. Then, we will create a sphere object using the pv.Sphere() function. Once the sphere object is ready, we will set up the plotter using the pv.Plotter() function. After the plotter is set, we will add the mesh to our sphere using the add_mesh() function. In this function, we will pass the sphere object, specify the desired color, and set show_edges=True to display the edges of the sphere. Finally, we will call the show() function to display the visualization.

import pyvista as pv

sphere = pv.Sphere()

plotter = pv.Plotter()
plotter.add_mesh(sphere, color="skyblue", show_edges=True)
plotter.show()

Output:

Structured Grid Visualization

To create a structured grid visualization, we will start by importing the required libraries, namely Numpy and PyVista. Then, we will create a structured grid data using Numpy's `arange` method to create a sequence of numbers from -10 to 10 with a step of 2 for the x, y, and z coordinates. We will use the `meshgrid` method from Numpy to create a rectangular grid out of these coordinates.

Once the structured grid data is ready, we create the structured grid object using PyVista's `StructuredGrid` class, passing our x, y, and z coordinates as parameters. After creating the structured grid, we will create a plotter object using the Plotter() function. We then add our structured grid to the plotter using the add_mesh() method, setting the color to light blue and enabling the display of edges. Finally, to display our structured grid visualization, we call the `show` method on our plotter object.

import numpy as np
import pyvista as pv

# Create structured grid data
x = np.arange(-10, 10, 2)
y = np.arange(-10, 10, 2)
z = np.arange(-10, 10, 2)
x, y, z = np.meshgrid(x, y, z)

# Create the structured grid
grid = pv.StructuredGrid(x, y, z)

# Plot the structured grid
plotter = pv.Plotter()
plotter.add_mesh(grid, color="lightblue", show_edges=True)
plotter.show()

Output:


Point Cloud

Now, let’s build a point cloud visualization, which is essentially a scatter plot in 3D. Point clouds are commonly used in 3D modeling and computer graphics. PyVista allows us to generate random point cloud data and visualize it in an interactive 3D space.

To build this visualization, we will first import the necessary libraries: NumPy and PyVista. Next, we will generate random point cloud data using NumPy’s rand() function. This data will then be converted into a PolyData object using PyVista’s PolyData() function. We will create a plotter object, just as before, and add the point cloud data using the add_mesh() function. We will set the color of the points to green and the size of the points to 5. Finally, we will use the show() function to display the plot.

import numpy as np
import pyvista as pv

# Generate random point cloud data
points = np.random.rand(100, 3)

# Create a PolyData object from the points
point_cloud = pv.PolyData(points)

# Plot the point cloud
plotter = pv.Plotter()
plotter.add_mesh(point_cloud, color="green", point_size=5, render_points_as_spheres=True)
plotter.show()

Output:

Knee Volume Visualization

Now, let's build an advanced 3D visualization: a 3D knee volume visualization. This visualization is crucial in medicine for analyzing and pinpointing areas of damage in the knee. Beyond the knees, it can be applied to visualize any part of the body to identify fractures or other issues. Let's proceed to learn how to create a 3D knee visualization.

First, we will import the necessary modules from the PyVista library. Then, we will download and load the knee volume data using the download_knee_full() function. Once the volume data is ready, we will set up a plotter using the Plotter() function. The main part involves adding the volume to the plot using add_volume(), where we will specify the colormap as 'coolwarm', set the opacity function to 'sigmoid', and enable the scalar bar. Finally, we will call the show() function to visualize the data.

import pyvista as pv
from pyvista import examples

# Load an example volume data
vol = examples.download_knee_full()

# Plot the volume with a transfer function
plotter = pv.Plotter()
plotter.add_volume(vol, cmap="coolwarm", opacity="sigmoid", show_scalar_bar=True)
plotter.show()

Output:


Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg