dm_control icon indicating copy to clipboard operation
dm_control copied to clipboard

[viewer] Importing viewer module on macos immediately kills maplotlib windows

Open ShravanTata opened this issue 2 years ago • 4 comments

On MacOS, importing the viewer module from dm_control immediately kills the matplotlib plot figures even when it should be ideally blocked with plt.show(). To reproduce the issue run the code below as a script:

import matplotlib
import matplotlib.pyplot as plt

from dm_control import viewer

if __name__ == '__main__':
    print(f"Matplotlib backed {matplotlib.get_backend()} ")
    plt.plot()
    plt.show(block=True)

NOTE : To reproduce the bug from the above script make sure that backend used by matplotlib (printed on the terminal) is MacOSX. When using other non default backends on MacOS the issue goes away.

Looking into it I found that the issue comes from https://github.com/deepmind/dm_control/blob/3c67a42d021b97808fcc075c0c468d78f0c2233f/dm_control/_render/glfw_renderer.py#L28 Seems like having glfw.init() run globally is the cause.

ShravanTata avatar Jan 27 '23 17:01 ShravanTata

Haven't had a chance to actually look into this yet, but regarding glfw.init(), that's the only way to run it. There isn't a "run it locally" option. If it is in fact glfw.init() that's causing the problem then it's likely an incompatibility between glfw and the particular matplotlib backend.

saran-t avatar Jan 27 '23 17:01 saran-t

Yes, it is definitely a problem particularly with the MacOSX backend which happens to be the default. When I switch to, for example Qt backend (to permanently do it add the following to your (bash)rc export MPLBACKEND=QtAgg) the issue goes away

ShravanTata avatar Jan 27 '23 18:01 ShravanTata

Are you getting any error message or does it just silently close?

saran-t avatar Jan 27 '23 18:01 saran-t

No errors/warnings are raised. It just silently closes

ShravanTata avatar Jan 27 '23 19:01 ShravanTata