PyVista surface with dynamic colormap
Posted on January 27, 2022
by Stéphane Laurent
I’ve just done this animation with PyVista:
This is a Hopf torus with the so-called flag
colormap as texture, but this texture moves. Here is a link to the full code.
The trick is to normalize the scalars to \([0, 2\pi]\) and then to apply the map \(\sin(\cdot - t)\) to the scalars, with \(t\) varying from \(0\) to \(2\pi\). Let me show you another example, with an ellipsoid.
from math import pi, sin
import numpy as np
import pyvista as pv
= pv.Sphere(theta_resolution=200, phi_resolution=200)
mesh 1, 0.5, 1), inplace=True) # ellipsoid
mesh.scale((= mesh.points[:,2] # we take the third coordinates for the scalars
dists # then we map them to [0, 2*pi]:
= 2*pi * (dists - dists.min()) / (dists.max() - dists.min())
dists
= np.linspace(0, 2*pi, 100, endpoint=False) # the varying parameter
t_ for i, t in enumerate(t_):
= "pic%03d.png" % i
pngfile = pv.Plotter(window_size = [512, 256], off_screen = True)
pltr = "#363940"
pltr.background_color 0, 0, 0])
pltr.set_focus([1.3, 0, 0))
pltr.set_position(("dist"] = np.sin(dists - t) # set the scalars - here is the "trick"
mesh[
pltr.add_mesh(= True, cmap = "flag",
mesh, smooth_shading = False, specular = 10
show_scalar_bar
)= pngfile) pltr.show(screenshot
This code produces the png
files pic000.png
, …, pic099.png
. Then it remains to make a gif
file from them, using e.g. ImageMagick or gifski. And here is the result: