Skip to content

Dedupe boilerplate for "adoption" of figure into pyplot. #16209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/matplotlib/_pylab_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,19 @@ def get_active(cls):
"""Return the active manager, or *None* if there is no manager."""
return next(reversed(cls.figs.values())) if cls.figs else None

@classmethod
def _set_new_active_manager(cls, manager):
"""Adopt *manager* into pyplot and make it the active manager."""
if not hasattr(manager, "_cidgcf"):
manager._cidgcf = manager.canvas.mpl_connect(
"button_press_event", lambda event: cls.set_active(manager))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Alternatively, this callback could be made "pickling-persistent" using the mechanism proposed in #16220.)

fig = manager.canvas.figure
fig.number = manager.num
label = fig.get_label()
if label:
manager.set_window_title(label)
cls.set_active(manager)

@classmethod
def set_active(cls, manager):
"""Make *manager* the active manager."""
Expand Down
19 changes: 2 additions & 17 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2009,24 +2009,9 @@ def __setstate__(self, state):
allnums = plt.get_fignums()
num = max(allnums) + 1 if allnums else 1
mgr = plt._backend_mod.new_figure_manager_given_figure(num, self)

# XXX The following is a copy and paste from pyplot. Consider
# factoring to pylab_helpers

if self.get_label():
mgr.set_window_title(self.get_label())

# make this figure current on button press event
def make_active(event):
pylab_helpers.Gcf.set_active(mgr)

mgr._cidgcf = mgr.canvas.mpl_connect('button_press_event',
make_active)

pylab_helpers.Gcf.set_active(mgr)
self.number = num

pylab_helpers.Gcf._set_new_active_manager(mgr)
plt.draw_if_interactive()

self.stale = True

def add_axobserver(self, func):
Expand Down
19 changes: 4 additions & 15 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,28 +520,17 @@ def figure(num=None, # autoincrement if None, else integer from 1-N
frameon=frameon,
FigureClass=FigureClass,
**kwargs)

fig = figManager.canvas.figure
if figLabel:
figManager.set_window_title(figLabel)
figManager.canvas.figure.set_label(figLabel)

# make this figure current on button press event
def make_active(event):
_pylab_helpers.Gcf.set_active(figManager)
fig.set_label(figLabel)

cid = figManager.canvas.mpl_connect('button_press_event', make_active)
figManager._cidgcf = cid

_pylab_helpers.Gcf.set_active(figManager)
fig = figManager.canvas.figure
fig.number = num
_pylab_helpers.Gcf._set_new_active_manager(figManager)

# make sure backends (inline) that we don't ship that expect this
# to be called in plotting commands to make the figure call show
# still work. There is probably a better way to do this in the
# FigureManager base class.
if matplotlib.is_interactive():
draw_if_interactive()
draw_if_interactive()

if _INSTALL_FIG_OBSERVER:
fig.stale_callback = _auto_draw_if_interactive
Expand Down