Type to search…
Skip to content

PyVista

3D scientific visualization: meshes, point clouds colored by a value, and animations.

Taught in
Disseny de nous fàrmacsAcoblament molecularDAW-BIO

Introduction

Matplotlib and Plotly make charts for you. PyVista does something else: three-dimensional objects that rotate, with light, with surfaces and with volume.

It’s what you need when your data isn’t a series of numbers but a shape — a mechanical part, a terrain, an MRI scan, the markers from a motion capture.

PyVista is a Python layer over VTK, the scientific visualization engine behind programs like ParaView. VTK is written in C++ and is twenty-five years old; PyVista is what makes it bearable.

It draws on your machine, which is what lets you move millions of triangles — and it’s also what makes it need a graphics card that actually works. For molecules, and especially for teaching them to someone else, py3Dmol draws in the browser and doesn’t depend on anything.

Working environment

ps
uv init viz
Initialized project `viz` at `C:\Users\eva\viz`

cd viz
uv add pyvista imageio
Using CPython 3.14.3
Resolved 32 packages in 337ms
Installed 31 packages in 95ms
 + imageio==2.37.4
 + pyvista==0.48.4

You only need imageio to save animations; to just look at things on screen, pyvista is enough.

Shapes

A Plotter is the window. You add meshes to it with add_mesh and open it with show.

python
shapes.py
import pyvista as pv

plotter = pv.Plotter()
plotter.set_background("white")

plotter.add_mesh(pv.Sphere(radius=1, center=(0, 0, 0)), color="#dd3333")
plotter.add_mesh(pv.Cube(center=(3, 0, 0)), color="#3366ee")
plotter.add_mesh(pv.Cone(center=(6, 0, 0), direction=(1, 0, 0)), color="#ddaa22")

plotter.show()

Drag the mouse to rotate it and the wheel to zoom in.

Notice that you haven’t placed any camera: show() looks at where things are and faces them on its own.

A point cloud

The most common form of scientific data isn’t a mesh but a list of points, each point with a measured value.

A PolyData is exactly that, and glyph places the same figure at each point — a sphere, repeated, that VTK draws in one go no matter how many there are.

python
cloud.py
from math import cos, sin

import pyvista as pv

points = []
heights = []
for i in range(600):
    angle = i * 0.12
    radius = 0.02 * i
    points.append([radius * cos(angle), radius * sin(angle), 0.01 * i])
    heights.append(0.01 * i)

cloud = pv.PolyData(points)
cloud["height"] = heights

spheres = cloud.glyph(geom=pv.Sphere(radius=0.25), orient=False, scale=False)
plotter = pv.Plotter()
plotter.set_background("white")
plotter.add_mesh(spheres, scalars="height", cmap="viridis", smooth_shading=True)
plotter.show()

Keep reading — it's free.

The rest of this page is open to anyone with a free account. Nothing is sold here and nothing is charged for: the account exists so we know who agreed to the terms, and so we can send you the newsletter if you want it.

Create a free account

You will be asked to accept the Terms · Privacy Policy