Skip to content

Commit 87bf41e

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 87bf41e

File tree

4 files changed

+58
-24
lines changed

4 files changed

+58
-24
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, *, create_if_none=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+
create_if_none : bool, default: True
1489+
*create_if_none* controls this method's behavior if there is no
1490+
current Axes: a new one is created if *create_if_none* is True (the
1491+
default); None is returned if *create_if_none* 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 create_if_none else
1514+
None)
15061515

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

lib/matplotlib/pyplot.py

+36-13
Original file line numberDiff line numberDiff line change
@@ -815,18 +815,22 @@ def _auto_draw_if_interactive(fig, val):
815815
fig.canvas.draw_idle()
816816

817817

818-
def gcf():
818+
def gcf(*, create_if_none=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+
create_if_none : bool, default: True
825+
*create_if_none* controls this method's behavior if there is no
826+
current figure: a new one is created with `~.pyplot.figure()`
827+
if *create_if_none* is True (the default); None is returned if
828+
*create_if_none* is False.
824829
"""
825830
manager = _pylab_helpers.Gcf.get_active()
826-
if manager is not None:
827-
return manager.canvas.figure
828-
else:
829-
return figure()
831+
return (manager.canvas.figure if manager is not None else
832+
figure() if create_if_none else
833+
None)
830834

831835

832836
def fignum_exists(num):
@@ -954,6 +958,31 @@ def figlegend(*args, **kwargs):
954958

955959
## Axes ##
956960

961+
def gca(*, create_if_none=True, **kwargs):
962+
"""
963+
Get the current Axes.
964+
965+
Parameters
966+
----------
967+
create_if_none : bool, default: True
968+
*create_if_none* controls this functions's behavior if there is no
969+
current Axes: a new one is created if *create_if_none* is True (the
970+
default); None is returned if *create_if_none* is False.
971+
972+
Other Parameters
973+
----------------
974+
**kwargs
975+
The kwargs listed below are supported for ensuring the returned
976+
Axes adheres to the given projection etc., and for Axes creation if
977+
the active Axes does not exist. This behavior is deprecated.
978+
979+
%(Axes:kwdoc)s
980+
"""
981+
fig = gcf(create_if_none=create_if_none)
982+
return (None if fig is None
983+
else fig.gca(create_if_none=create_if_none, **kwargs))
984+
985+
957986
@docstring.dedent_interpd
958987
def axes(arg=None, **kwargs):
959988
"""
@@ -2484,12 +2513,6 @@ def figtext(x, y, s, fontdict=None, **kwargs):
24842513
return gcf().text(x, y, s, fontdict=fontdict, **kwargs)
24852514

24862515

2487-
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
2488-
@_copy_docstring_and_deprecators(Figure.gca)
2489-
def gca(**kwargs):
2490-
return gcf().gca(**kwargs)
2491-
2492-
24932516
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
24942517
@_copy_docstring_and_deprecators(Figure._gci)
24952518
def gci():

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(create_if_none=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(create_if_none=False) is None
155+
153156
with pytest.raises(TypeError):
154157
assert fig.add_axes() is None
155158

tools/boilerplate.py

-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ def boilerplate_gen():
203203
_figure_commands = (
204204
'figimage',
205205
'figtext:text',
206-
'gca',
207206
'gci:_gci',
208207
'ginput',
209208
'subplots_adjust',

0 commit comments

Comments
 (0)