Slicer 4.11 closes after closing the Figure Window for matplotlib

Hi all,

I work on the recent 4.11 (06.24.2020) and Slicer closes right after I close the window from matplotlib.
For your convenience I use that piece of code in Python Interactor:

from matplotlib import pyplot as plt
fig, axs = plt.subplots(1)

and a window opens. Right after I close it, Slicer closes as well.

Do you think it is a bug?


Could you post a complete example (including pip_install and setting of rendering backend)?

If you use matplotlib with a non-gui back end you can save to png and view it with vtk.

There’s an old example here from slicer3, but the same idea should work with the current Slicer.

The following steps are:

#pip_install("matplotlib wxPython")

import matplotlib
from matplotlib import pyplot as plt
fig, axs = plt.subplots(1)

and still have the same issue, I re-installed the .exe file but still the same issue.

Note: I use the same sample code at 4.11 (3.10.2020) with no issue.

For static plots, you can use “Agg” backend. It still works well - see example here.

Using matplotlib for interactive plots in a GUI application is very unclear, so I would not recommend doing it. Instead I would recommend using VTK plots, as they are faster and they integrate much nicer into 3D Slicer GUI and it allows you to directly interact with data stored in the scene.

If for some reason you want to still try to use matplotlib for interactive rendering then one workaround is to show the plot using, hide the close button on the figure image somehow (to make sure users don’t accidentally push it) and use plt.close() to cleanly close the plot. You could also try other matplotlib rendering backends (such as qt4 or qt5).

What would you like to plot interactively?

The only interactive functionality I want for now to have is the zoom in and show the coordinates once the cursor hovers the points. Maybe later to be able to show and hide some graphs at the same figure. Thus, I don’t think that png photos would give me that functionality.

I am wondering why at a latest version was working fine (4.11 03.10.2020), Do you think it is related to Slicer or matplotlib?

1 Like