# Slicer 4.11 closes after closing the Figure Window for matplotlib

**URL:** https://discourse.slicer.org/t/slicer-4-11-closes-after-closing-the-figure-window-for-matplotlib/12236
**Category:** Support
**Created:** [June 26, 2020, 5:10pm UTC](https://discourse.slicer.org/t/slicer-4-11-closes-after-closing-the-figure-window-for-matplotlib/12236 "2020-06-26T17:10:34Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![siaeleni](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@siaeleni](https://discourse.slicer.org/u/siaeleni)
#### Post date: [June 26, 2020, 5:10pm UTC](https://discourse.slicer.org/t/slicer-4-11-closes-after-closing-the-figure-window-for-matplotlib/12236/1 "2020-06-26T17:10:34Z")

</div>

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)
plt.show()

```

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

Do you think it is a bug?

Thanks,  
Eleni

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [June 26, 2020, 5:30pm UTC](https://discourse.slicer.org/t/slicer-4-11-closes-after-closing-the-figure-window-for-matplotlib/12236/2 "2020-06-26T17:30:01Z")

</div>

Could you post a complete example (including pip\_install and setting of rendering backend)?

---

<div class="post-metadata">

### Author: ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)
#### Post date: [June 26, 2020, 6:03pm UTC](https://discourse.slicer.org/t/slicer-4-11-closes-after-closing-the-figure-window-for-matplotlib/12236/3 "2020-06-26T18:03:51Z")

</div>

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.

[https://www.slicer.org/wiki/Slicer3:Python#Matplotlib\_plotting\_functionality](https://www.slicer.org/wiki/Slicer3:Python#Matplotlib_plotting_functionality)

---

<div class="post-metadata">

### Author: ![siaeleni](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@siaeleni](https://discourse.slicer.org/u/siaeleni)
#### Post date: [June 26, 2020, 6:10pm UTC](https://discourse.slicer.org/t/slicer-4-11-closes-after-closing-the-figure-window-for-matplotlib/12236/4 "2020-06-26T18:10:53Z")

</div>

The following steps are:

```
#pip_install('matplotlib')
#pip_install("matplotlib wxPython")

import matplotlib
matplotlib.use('WXAgg')
from matplotlib import pyplot as plt
fig, axs = plt.subplots(1)
plt.show()

```

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.

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [June 26, 2020, 7:33pm UTC](https://discourse.slicer.org/t/slicer-4-11-closes-after-closing-the-figure-window-for-matplotlib/12236/5 "2020-06-26T19:33:26Z")

</div>

For static plots, you can use “Agg” backend. It still works well - see example [here](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Non-interactive_plot).

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](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Slicer_plots_displayed_in_view_layout), 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 `plt.show(block=False)`, 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?

---

<div class="post-metadata">

### Author: ![siaeleni](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@siaeleni](https://discourse.slicer.org/u/siaeleni)
#### Post date: [June 27, 2020, 3:40pm UTC](https://discourse.slicer.org/t/slicer-4-11-closes-after-closing-the-figure-window-for-matplotlib/12236/6 "2020-06-27T15:40:33Z")

</div>

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?
