Creating Interactive 3D Visualizations using PyVista
Last Updated : 01 Jul, 2024
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.
Python 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.
Python 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.
Python 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.
Python 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:
Similar Reads
Interactive visualization of data using Bokeh Bokeh is a Python library for creating interactive data visualizations in a web browser. It offers human-readable and fast presentation of data in an visually pleasing manner. If youâve worked with visualization in Python before, itâs likely that you have used matplotlib. But Bokeh differs from matp
4 min read
Interactive Data Visualization with Python and Bokeh In this article, we'll learn how to do Interactive Data Visualization with Bokeh. Bokeh is a Python library that is used for creating interactive visualizations for modern web browsers. It handles custom or specialized use cases very simply. Â It provides high-performance interactive charts and plots
8 min read
Data Visualization using Matplotlib in Python Matplotlib is a widely-used Python library used for creating static, animated and interactive data visualizations. It is built on the top of NumPy and it can easily handles large datasets for creating various types of plots such as line charts, bar charts, scatter plots, etc. These visualizations he
10 min read
How to Use PyVista Plotter for 3D Visualization in Python In Python, PyVista is a powerful library for 3D visualization and mesh analysis. It integrates seamlessly with NumPy and provides a robust set of tools for creating interactive plots, mesh generation, and advanced visualization techniques. PyVista's Plotter class offers a versatile environment to cr
3 min read
Creating Dynamic Visualizations using IPython Notebook Widget In this article, we learn about how to create dynamic visualizations using the IPython Notebook Widget and its examples. In this article, we cover the following points: Dynamic Visualizations Information analysis and scientific calculation are at the centre of numerous study and improvement undertak
12 min read