1
$\begingroup$

I'd like to visualize scalar data located on the nodes of a quad mesh (2D) using VTK in Python3. The closest example I've found is vtk quad mesh, but it only seems to visualize the mesh, not data on the mesh. Could someone point me to a better example?

$\endgroup$
1
  • 2
    $\begingroup$ I recommend using ParaView which is kinda act likes a GUI for VTK if you are not familiar with VTK API. In ParaView, it's really easy. You need to open your file whatever it is and just visualize it. $\endgroup$ – Alone Programmer Nov 12 '20 at 14:57
2
$\begingroup$

As mentioned by @AloneProgrammer in the comments, you could use ParaView since it is effortless. As added by @WolfgangBangerth, you could also try VisIt that provides a frontend for VTK algorithms.

If you need to do it programmatically through Python scripts you could use:

  • VTK itself. The API is not the most intuitive but you could get used to it. It gives you control over every detail.

  • PyVista provides an easy to use API.

  • Mayavi is also easy to use.

$\endgroup$
2
  • 1
    $\begingroup$ There's also Visit, which has essentially the same functionality as Paraview but a different user interface. $\endgroup$ – Wolfgang Bangerth Nov 12 '20 at 16:48
  • 1
    $\begingroup$ Yes, I should add it to the answer $\endgroup$ – nicoguaro Nov 13 '20 at 2:44
2
$\begingroup$

Here's a simple working example of what I wanted to do, using PyVista. It needs X11 to plot. So, if you are running on a system without X11 (e.g. Ubuntu under WSL2) you might need to install Xvfb. See here for further instructions.

This example is based on this PyVista example

import numpy as np
import pyvista as pv

# mesh points
vertices = np.array([[0, 0, 0],
                     [1, 0, 0],
                     [1, 1, 0],
                     [0, 1, 0],
                     [2, 0, 0],
                     [3, 1, 0]])

# mesh faces
# the 4 indicates how many nodes are present
# other numbers are indicies in the vertices array
faces = np.hstack([[4, 0, 1, 2, 3],
                   [4, 1, 4, 5, 2]])   

# define a surface or as PyVista calls it a 'mesh' object
surf = pv.PolyData(vertices, faces)
# define nodal/point data
surf.point_arrays['point1'] = np.arange(surf.n_points)
# define cell/element data
surf.cell_arrays['cell1']   = np.arange(surf.n_cells)

# parameters for the colorbar
sargs = dict(height=0.25, vertical=True, position_x=0.85, position_y=0.05)

# create the plotter
p = pv.Plotter()
# show the axes widget 
p.show_axes()
# add the mesh to the plotter and tell it which field to display
# and set the arguments for the scalar bar
p.add_mesh(surf,scalars='point1',scalar_bar_args=sargs)
# specify the view
p.view_xy()
# plot and take a screenshot
p.show(screenshot='pyvistatest.png')
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.