Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d496e77

Browse files
committed
Simplify get_current_figure_manager().
Also make ``plt.subplot_tool`` work even for non-pyplot-managed figures -- there's no reason not to, it's just that such figures cannot be made the "current" pyplot figure.
1 parent c1a3c03 commit d496e77

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

examples/misc/agg_buffer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
plt.plot([1, 2, 3])
1616

17-
canvas = plt.get_current_fig_manager().canvas
17+
canvas = plt.gcf().canvas
1818

1919
agg = canvas.switch_backends(FigureCanvasAgg)
2020
agg.draw()

lib/matplotlib/pyplot.py

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -607,21 +607,17 @@ def get_current_fig_manager():
607607
608608
If there is currently no active figure, a new one is created.
609609
"""
610-
figManager = _pylab_helpers.Gcf.get_active()
611-
if figManager is None:
612-
gcf() # creates an active figure as a side effect
613-
figManager = _pylab_helpers.Gcf.get_active()
614-
return figManager
610+
return gcf().canvas.manager
615611

616612

617613
@docstring.copy(FigureCanvasBase.mpl_connect)
618614
def connect(s, func):
619-
return get_current_fig_manager().canvas.mpl_connect(s, func)
615+
return gcf().canvas.mpl_connect(s, func)
620616

621617

622618
@docstring.copy(FigureCanvasBase.mpl_disconnect)
623619
def disconnect(cid):
624-
return get_current_fig_manager().canvas.mpl_disconnect(cid)
620+
return gcf().canvas.mpl_disconnect(cid)
625621

626622

627623
def close(fig=None):
@@ -683,7 +679,7 @@ def draw():
683679
This is equivalent to calling ``fig.canvas.draw_idle()``, where ``fig`` is
684680
the current figure.
685681
"""
686-
get_current_fig_manager().canvas.draw_idle()
682+
gcf().canvas.draw_idle()
687683

688684

689685
@docstring.copy(Figure.savefig)
@@ -1262,25 +1258,19 @@ def subplot_tool(targetfig=None):
12621258
12631259
A :class:`matplotlib.widgets.SubplotTool` instance is returned.
12641260
"""
1265-
tbar = rcParams['toolbar'] # turn off navigation toolbar for the toolfig
1266-
rcParams['toolbar'] = 'None'
12671261
if targetfig is None:
1268-
manager = get_current_fig_manager()
1269-
targetfig = manager.canvas.figure
1270-
else:
1271-
# find the manager for this figure
1272-
for manager in _pylab_helpers.Gcf._activeQue:
1273-
if manager.canvas.figure == targetfig:
1274-
break
1275-
else:
1276-
raise RuntimeError('Could not find manager for targetfig')
1262+
targetfig = gcf()
12771263

1264+
tbar = rcParams['toolbar'] # Turn off navigation toolbar for the toolfig.
1265+
rcParams['toolbar'] = 'None'
12781266
toolfig = figure(figsize=(6, 3))
12791267
toolfig.subplots_adjust(top=0.9)
1280-
ret = SubplotTool(targetfig, toolfig)
12811268
rcParams['toolbar'] = tbar
1282-
_pylab_helpers.Gcf.set_active(manager) # restore the current figure
1283-
return ret
1269+
1270+
if hasattr(targetfig.canvas, "manager"): # Restore the current figure.
1271+
_pylab_helpers.Gcf.set_active(targetfig.canvas.manager)
1272+
1273+
return SubplotTool(targetfig, toolfig)
12841274

12851275

12861276
def tight_layout(pad=1.08, h_pad=None, w_pad=None, rect=None):

0 commit comments

Comments
 (0)