pythonpyvista

How to access various Plotter methods outside my class?


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?


Solution

  • 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.