Mayavi colorbar in TraitsUI creating blank window
Asked Answered
S

1

84

I'm trying to create a GUI in TraitsUI that includes two Mayavi figures. I have implemented these figures as per the multiple engines example in the Mayavi documentation.

However, when I add a colorbar to one of the figures and run the GUI script it sometimes opens a blank Mayavi Scene Editor window in addition to the desired TraitsUI window. This blank window doesn't always appear, never on the first run after restarting the python kernel, and sometimes only after running the script a few times in succession and closing the windows that appear each time.

Running the much-reduced code below produces the same behaviour, and removing the mlab.colorbar(s) line stops the problem. How can I get a colorbar without opening blank windows? There doesn't seem to be an obvious way to assign a colorbar to a specific figure as for the surface plot. I am running Python 3.5 on Windows 7 (but get the same issues on Ubuntu).

from traits.api import HasTraits, Instance, on_trait_change
from traitsui.api import View, Item
import numpy as np

from mayavi.core.api import Engine
from mayavi.core.ui.api import SceneEditor, MlabSceneModel
from mayavi import mlab

#Generate a test surface to display
def test_surf():
    x, y = np.mgrid[-7.:7.05:0.1, -5.:5.05:0.05]
    z = np.sin(x + y) + np.sin(2 * x - y) + np.cos(3 * x + 4 * y)
    return x, y, z        

class MyApp(HasTraits):

    #Create a mayavi scene with a specified engine
    engine = Instance(Engine, ())
    scene = Instance(MlabSceneModel)
    def _scene_default(self):
        self.engine.start()
        return MlabSceneModel(engine=self.engine)         

    #Plot the surface when the scene is activated
    @on_trait_change('scene.activated')
    def populate_scene(self):
        s = mlab.surf(*test_surf(), figure=self.scene.mayavi_scene)
        mlab.colorbar(s)

    view = View(Item('scene', editor=SceneEditor()))

if __name__ == '__main__':
    MyApp().configure_traits()
Singular answered 24/8, 2017 at 18:30 Comment(0)
A
1

You may add something that closes/quits the windows you invoke.

For example you could close the figure self.scene.mayavi_scene using function mayavi.mlab.close.

Ariellearies answered 4/4, 2021 at 19:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.