1

I'm plotting a mesh with PyVista using UnstructuredGrid. It's going pretty well and I managed to plot the geometry I want using grid.plot().

I now want to access methods outside of UnstructuredGrid: for example, I want to be able to click and select on mesh elements and I see this is possible to do with the enable_point_picking method. The only problem is that this method does not belong to UnstructuredGrid, but to the Plotter class.

Does this mean I cannot use that method, or are there any workarounds?

man-teiv
  • 419
  • 3
  • 16
  • 1
    How are you plotting your unstructured grid? With `grid.plot()`? Because that's just a convenience function that creates a `Plotter` for you. Without your adding some more details it's hard to say where your exact issue is. – Andras Deak -- Слава Україні Apr 05 '23 at 20:36
  • This is correct, I'm using `grid.plot()`. How can I access the Plotter it's creating? Is it as simple as assigning `plotter = grid.plot()`? And thanks for the reply – man-teiv Apr 06 '23 at 08:04

1 Answers1

2

Here is the documentation of the .plot() methods you're using. These methods are actually just pyvista.plot():

>>> import pyvista as pv
>>> pv.PolyData.plot is pv.plot
True

This helper function is only for convenience, sparing you the trouble of setting up a Plotter to get a quick plot of a mesh. If you consult the documentation you'll see that there's no option to return that plotter object, so no, you can't directly use grid.plot() if you want additional customization not offered by grid.plot().

But not to worry: it's very easy to create your own plotter:

import pyvista as pv

grid = pv.Dodecahedron()
plotter = pv.Plotter()
plotter.add_mesh(grid)
plotter.show()

screenshot of dodecahedron with coloured faces and a scalar bar showing face indices as values

Now you can customize the plotter in any way you desire, e.g. by passing show_scalar_bar=False to add_mesh() in order to disable the scalar bar. Plotter's documentation is here, and specifically the docs of Plotter.add_mesh() is here. There are a lot of options, so I recommend taking your time to slowly peruse them to learn what kind of things you can easily do. And of course you have the entire Plotter API to use, including the enable_point_picking you mentioned in your question. There's a lot of things PyVista can do for you, so you should stop and learn to carefully read documentation.

  • 1
    Thank you so much. This was extremely helpful in understanding how PyVista works. I have some work to do in reading the docs. Cheers! – man-teiv Apr 07 '23 at 11:43