Skip to content

Commit ed8cbbd

Browse files
committed
Add option to gcf/gca to disable new figure/axes creation.
This provides a relatively more user-friendly way to check whether a current figure/axes exists. (`_pylab_helpers.Gcf.get_active` is way too "internal".)
1 parent f8f6939 commit ed8cbbd

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

lib/matplotlib/figure.py

+19-10
Original file line numberDiff line numberDiff line change
@@ -1479,15 +1479,25 @@ def sca(self, a):
14791479
return a
14801480

14811481
@docstring.dedent_interpd
1482-
def gca(self, **kwargs):
1482+
def gca(self, *, can_be_new=True, **kwargs):
14831483
"""
1484-
Get the current Axes, creating one if necessary.
1484+
Get the current Axes.
14851485
1486-
The following kwargs are supported for ensuring the returned Axes
1487-
adheres to the given projection etc., and for Axes creation if
1488-
the active Axes does not exist:
1486+
Parameters
1487+
----------
1488+
can_be_new : bool, default: True
1489+
*can_be_new* controls this method's behavior if there is no current
1490+
Axes: a new one is created if *can_be_new* is True (the default);
1491+
None is returned if *can_be_new* is False.
14891492
1490-
%(Axes:kwdoc)s
1493+
Other Parameters
1494+
----------------
1495+
**kwargs
1496+
The kwargs listed below are supported for ensuring the returned
1497+
Axes adheres to the given projection etc., and for Axes creation if
1498+
the active Axes does not exist. This behavior is deprecated.
1499+
1500+
%(Axes:kwdoc)s
14911501
"""
14921502
if kwargs:
14931503
_api.warn_deprecated(
@@ -1499,10 +1509,9 @@ def gca(self, **kwargs):
14991509
"new axes with default keyword arguments. To create a new "
15001510
"axes with non-default arguments, use plt.axes() or "
15011511
"plt.subplot().")
1502-
if self._axstack.empty():
1503-
return self.add_subplot(1, 1, 1, **kwargs)
1504-
else:
1505-
return self._axstack()
1512+
return (self._axstack() if not self._axstack.empty() else
1513+
self.add_subplot(1, 1, 1, **kwargs) if can_be_new else
1514+
None)
15061515

15071516
def _gci(self):
15081517
# Helper for `~matplotlib.pyplot.gci`. Do not use elsewhere.

lib/matplotlib/pyplot.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -815,18 +815,21 @@ def _auto_draw_if_interactive(fig, val):
815815
fig.canvas.draw_idle()
816816

817817

818-
def gcf():
818+
def gcf(*, can_be_new=True):
819819
"""
820820
Get the current figure.
821821
822-
If no current figure exists, a new one is created using
823-
`~.pyplot.figure()`.
822+
Parameters
823+
----------
824+
can_be_new : bool, default: True
825+
*can_be_new* controls this method's behavior if there is no current
826+
figure: a new one is created with `~.pyplot.figure()` if *can_be_new*
827+
is True (the default); None is returned if *can_be_new* is False.
824828
"""
825829
manager = _pylab_helpers.Gcf.get_active()
826-
if manager is not None:
827-
return manager.canvas.figure
828-
else:
829-
return figure()
830+
return (manager.canvas.figure if manager is not None else
831+
figure() if can_be_new else
832+
None)
830833

831834

832835
def fignum_exists(num):
@@ -2486,8 +2489,8 @@ def figtext(x, y, s, fontdict=None, **kwargs):
24862489

24872490
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
24882491
@_copy_docstring_and_deprecators(Figure.gca)
2489-
def gca(**kwargs):
2490-
return gcf().gca(**kwargs)
2492+
def gca(*, can_be_new=True, **kwargs):
2493+
return gcf().gca(can_be_new=can_be_new, **kwargs)
24912494

24922495

24932496
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.

lib/matplotlib/tests/test_figure.py

+3
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def test_align_labels():
6464
def test_figure_label():
6565
# pyplot figure creation, selection, and closing with label/number/instance
6666
plt.close('all')
67+
assert plt.gcf(can_be_new=False) is None
6768
fig_today = plt.figure('today')
6869
plt.figure(3)
6970
plt.figure('tomorrow')
@@ -150,6 +151,8 @@ def test_figure_legend():
150151
def test_gca():
151152
fig = plt.figure()
152153

154+
assert plt.gca(can_be_new=False) is None
155+
153156
with pytest.raises(TypeError):
154157
assert fig.add_axes() is None
155158

0 commit comments

Comments
 (0)