Search code examples
pythonpyvista

Multi-window plot with PyVista are (wrongly) sharing the colorlevels


I want to make a multi-window plot of varies slices of 3D volumetric data. I found PyVista to be what I was looking for. When I am doing just a single plot, everything is fine. I have a problem, however, when I am plotting multiple slices in one window: for some reason, the subplots are sharing the colorlevels, which can lead to one subplot basically showing nothing, as illustrated in the plot.

Any idea what I am missing here would be greatly appreciated!

This is the code (and an image should be attached):

import numpy as np
import pyvista as pv

Nx, Ny, Nz  = 100, 100, 200

plotter    = pv.Plotter(shape=(1,2))

plotter.subplot(0,0)
vol1    = pv.wrap( np.random.randint(10, size=(Nx,Ny,Nz)) )
slic1   = vol1.slice_orthogonal()
plotter.add_mesh(slic1)

plotter.subplot(0,1)
vol2    = pv.wrap( np.random.randint(100, size=(Nx,Ny,Nz)) )
slic2   = vol2.slice_orthogonal()
plotter.add_mesh(slic2)

plotter.show()

enter image description here


Solution

  • The problem is that scalars for a scalar bar are stored in a dict, with the scalar bar title as the key. When you don't set a scalar bar title yourself, the default of '' is used, see Plotter.add_scalar_bar(). Since both datasets share the same empty string as key, the latter overwrites the former.

    The solution is to pass an explicit title for your scalar bars:

    import numpy as np
    import pyvista as pv
    
    Nx, Ny, Nz  = 100, 100, 200
    
    plotter    = pv.Plotter(shape=(1,2))
    
    plotter.subplot(0,0)
    vol1    = pv.wrap( np.random.randint(10, size=(Nx,Ny,Nz)) )
    slic1   = vol1.slice_orthogonal()
    plotter.add_mesh(slic1, scalar_bar_args={'title': 'Vol1'})
    
    plotter.subplot(0,1)
    vol2    = pv.wrap( np.random.randint(100, size=(Nx,Ny,Nz)) )
    slic2   = vol2.slice_orthogonal()
    plotter.add_mesh(slic2, scalar_bar_args={'title': 'Vol2'})
    
    plotter.show()
    

    screenshot where both subplots have a colorbar with sane limits and colouring on the corresponding meshes