Skip to content

Commit

Permalink
ENH: add top-level function to fully demote a figure
Browse files Browse the repository at this point in the history
  • Loading branch information
tacaswell committed Nov 9, 2023
1 parent 7d017c9 commit 374c2af
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion mpl_gui/_promotion.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,27 @@ def _destroy_on_hotkey(event):

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

return manager


def demote_figure(fig):
"""Fully clear all GUI elements from the `~matplotlib.figure.Figure`.
The opposite of what is done during `mpl_gui.display`.
Parameters
----------
fig : matplotlib.figure.Figure
"""
fig.canvas.destroy()
fig.canvas.manager = None
original_dpi = getattr(fig, "_original_dpi", fig.dpi)
if (cid := getattr(fig, '_destroy_cid', None)) is not None:
fig.canvas.mpl_disconnect(cid)
FigureCanvasBase(fig)
fig.dpi = original_dpi

return fig

0 comments on commit 374c2af

Please sign in to comment.