0

I need to plot data that is living in Python (or some file that I read from python) and not living in a VTK file. I would like to achieve the equivalent of this simple matplotlib script using VTK with vectorized input (preferably numpy arrays). I would like to be able to update this directly from the application in a responsive manner (i.e. the pattern changing in response to some parameter on the UI)

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# make some interesting data
theta = np.linspace(-np.pi/2, np.pi/2, 181)
phi = np.arange(np.pi, -np.pi, -np.pi/180)[::-1]
r = np.outer(np.sin(2*phi), np.sin(2*theta))

# xyz it
x = r * np.outer(np.cos(phi), np.sin(theta))
y = r * np.outer(np.sin(phi), np.sin(theta))
z = r * np.outer(np.ones(np.size(phi)), np.cos(theta))

# plot the surface
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x, y, z, color='b')

plt.show()

enter image description here

I have found plenty of examples showing how to get data out of VTK structures into Python, but my requirement is the other way around. I have also found examples of how to build up VTK arrays element by element, but this is Python; I need it vectorized as I want it to be updated responsively. I also haven't been able to figure out how to get from a 2D StructuredGrid to a spherical plot.

1 Answer 1

1

Maybe this example can help:

from vedo import *
from vedo.pyplot import plot
import numpy as np

def rhofunc(theta, phi):
    if theta < 0.2:
        return np.nan # make some points invalid
    return (3*cos(theta)**2 - 1)**2 # Y(l=2 m=0)

# Build the plot, return a vtkAssembly
spl = plot(rhofunc, mode='spheric', cmap='viridis')
show(spl, axes=12)

enter image description here

4
  • Thanks for your response. I had a look into it. Under the hood this thing uses a function called _plotFxy. At github.com/marcomusy/vtkplotter/blob/master/vtkplotter/… line 763 is (currently) exactly the kind of Python for loop I'm wanting to avoid. The call to vtkplotter is vectorized, but not the call to vtk
    – Mark
    Commented May 5, 2020 at 12:38
  • Hi, I think the loop is actually the one at line 1063. Are you sure that vectorizing the loop would make any difference? the bottleneck is the rendering part
    – mmusy
    Commented May 5, 2020 at 15:36
  • I don't have a lot experience with VTK, but I'm thinking rendering performance could be scaled up with graphics hardware, whereas that is not so much the case with python. Am I mistaken? The data I wish to plot (at the moment) would have about 65000 points and I'm wanting to update it (performantly) using a slider (filtering from a much larger dataset). I'm not sure what the VTK performance on this will be, but I think looping over a 65000 element array in python = forget it
    – Mark
    Commented May 6, 2020 at 2:54
  • 1
    I've checked that looping on 70k elements with calling the same r(theta phi) function in the example above costs 0.3s on my average desktop. You can try to vectorize the function evaluation (if that's possible in your specific case) but the loop per se is not the bottleneck.
    – mmusy
    Commented May 6, 2020 at 10:16

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.