Skip to content

Commit 374c2af

Browse files
committed
ENH: add top-level function to fully demote a figure
1 parent 7d017c9 commit 374c2af

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

mpl_gui/_promotion.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,27 @@ def _destroy_on_hotkey(event):
8383

8484
# remove this callback. Callbacks live on the Figure so survive the canvas
8585
# being replaced.
86-
fig.canvas.mpl_connect("key_press_event", _destroy_on_hotkey)
86+
fig._destroy_cid = fig.canvas.mpl_connect("key_press_event", _destroy_on_hotkey)
8787

8888
return manager
89+
90+
91+
def demote_figure(fig):
92+
"""Fully clear all GUI elements from the `~matplotlib.figure.Figure`.
93+
94+
The opposite of what is done during `mpl_gui.display`.
95+
96+
Parameters
97+
----------
98+
fig : matplotlib.figure.Figure
99+
100+
"""
101+
fig.canvas.destroy()
102+
fig.canvas.manager = None
103+
original_dpi = getattr(fig, "_original_dpi", fig.dpi)
104+
if (cid := getattr(fig, '_destroy_cid', None)) is not None:
105+
fig.canvas.mpl_disconnect(cid)
106+
FigureCanvasBase(fig)
107+
fig.dpi = original_dpi
108+
109+
return fig

0 commit comments

Comments
 (0)