# Closing mayavi figure

**URL:** <https://mne.discourse.group/t/closing-mayavi-figure/3819>\
**Category:** Support & Discussions\
**Tags:** visualization, coregistration\
**Created:** [October 15, 2021, 7:43am UTC](https://mne.discourse.group/t/closing-mayavi-figure/3819 "2021-10-15T07:43:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlexLepauvre](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/alexlepauvre/32/254_2.png) [@AlexLepauvre](https://mne.discourse.group/u/AlexLepauvre)\
**Post date:** [October 15, 2021, 7:43am UTC](https://mne.discourse.group/t/closing-mayavi-figure/3819/1 "2021-10-15T07:43:19Z")

</div>

Dear support,

I am facing issues closing mayavi figures opened with the mne command mne.viz.plot\_alignment. My system specs are those:

- MNE-Python version: 0.23.0
- operating system: linux/windows

I am working with ecog data and need to open a lot of different mayavi 3D plots, as many as I have participants, all within the same script. And because I haven’t figured out how to close them within a script, so many plots remain open and my computers end up freezing.

I am sure there must exist an easy command to close mayavi 3D figures, but none of the ones I could find in their documentation seems to work. Has anyone figured out a way to do so and if yes how?

Kind regards,

Alex

PS: I haven’t included any code, ass it is a very simple issue: after calling the plot\_alignment function, how do I close the generated figure. If you would like some code, I am happy to provide some.

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [October 15, 2021, 9:04am UTC](https://mne.discourse.group/t/closing-mayavi-figure/3819/2 "2021-10-15T09:04:22Z")

</div>

Hello @AlexLepauvre,

in `mne.Report`, we are [basically](https://github.com/mne-tools/mne-python/blob/022f1a8f69c52062f956f015fba3c3b6f5ee7a1d/mne/report/report.py#L376-L387) doing the following (I hope I got this right, I made some untested modifications…):

```python
from mne.viz import create_3d_figure, plot_alignment
from mne.viz.backends.renderer import backend

fig = create_3d_figure((800, 800), bgcolor=(0.5, 0.5, 0.5))

try:
    try:
        plot_alignment(fig=fig, surfaces=['head-dense'], ...)
    except IOError:
        plot_alignment(fig=fig, surfaces=['head'], ...)
finally:
    backend._close_3d_figure(fig)

```

I think the key is the `backend._close_3d_figure(fig)` call.

also cc’ing @GuillaumeFavelier here, our 3D viz expert!

Best wishes,  
Richard

---

<div class="post-metadata">

**Author:** ![GuillaumeFavelier](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/guillaumefavelier/32/40_2.png) [@GuillaumeFavelier](https://mne.discourse.group/u/GuillaumeFavelier)\
**Post date:** [October 15, 2021, 9:21am UTC](https://mne.discourse.group/t/closing-mayavi-figure/3819/3 "2021-10-15T09:21:43Z")

</div>

Exactly @richard ! Either `_close_3d_figure(fig)` or `_close_all()` should do the job 👍

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [October 15, 2021, 9:28am UTC](https://mne.discourse.group/t/closing-mayavi-figure/3819/4 "2021-10-15T09:28:43Z")

</div>

Since we now have a user request, do you think it would make sense to make these functions public?

---

<div class="post-metadata">

**Author:** ![AlexLepauvre](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/alexlepauvre/32/254_2.png) [@AlexLepauvre](https://mne.discourse.group/u/AlexLepauvre)\
**Post date:** [October 15, 2021, 9:50am UTC](https://mne.discourse.group/t/closing-mayavi-figure/3819/5 "2021-10-15T09:50:22Z")

</div>

Thanks a lot everyone, this is immensely helpful 🙂

---

<div class="post-metadata">

**Author:** ![GuillaumeFavelier](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/guillaumefavelier/32/40_2.png) [@GuillaumeFavelier](https://mne.discourse.group/u/GuillaumeFavelier)\
**Post date:** [October 15, 2021, 9:55am UTC](https://mne.discourse.group/t/closing-mayavi-figure/3819/6 "2021-10-15T09:55:02Z")

</div>

If this is useful, I don’t see why it can’t be exposed. The implementation of `_close_all()` and `_close_3d_figure()` did not change in a while.

`set_3d_title()` and `set_3d_view()` were exposed in the same way.

> Thanks a lot everyone, this is immensely helpful

Glad that it works for you!

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [October 15, 2021, 5:17pm UTC](https://mne.discourse.group/t/closing-mayavi-figure/3819/7 "2021-10-15T17:17:58Z")

</div>

I opened a feature request to make those `close_` functions public

> <https://github.com/mne-tools/mne-python/issues/9855>
>
> As per discussion at https://mne.discourse.group/t/closing-mayavi-figure/3819/4
> …
> 
> cc @GuillaumeFavelier
