Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Creating Interactive 3D Visualizations using PyVista
Next article icon

Creating Interactive 3D Visualizations using PyVista

Last Updated : 01 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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:


Next Article
Creating Interactive 3D Visualizations using PyVista

A

adilnaib
Improve
Article Tags :
  • Python
  • Python Data Visualization
Practice Tags :
  • python

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences