Skip to content

Commit f26efd9

Browse files
committed
MNT: defer calling gca or gcf unless we absolutely must
1 parent 27540bb commit f26efd9

File tree

1 file changed

+46
-25
lines changed

1 file changed

+46
-25
lines changed

lib/matplotlib/pyplot.py

+46-25
Original file line numberDiff line numberDiff line change
@@ -712,9 +712,31 @@ def draw(fig=None):
712712
fig.canvas.draw_idle()
713713

714714

715+
def _pop_or_gca(kwargs):
716+
"""
717+
Internal helper function for getting an optional ax kwarg or gca
718+
719+
.. warning::
720+
721+
This mutates the input in place!
722+
"""
723+
return kwargs.pop('ax', None) or gca()
724+
725+
726+
def _pop_or_gcf(kwargs):
727+
"""
728+
Internal helper function for getting an optional fig kwarg or gcf
729+
730+
.. warning::
731+
732+
This mutates the input in place!
733+
"""
734+
return kwargs.pop('fig', None) or gcf()
735+
736+
715737
@docstring.copy_dedent(Figure.savefig)
716738
def savefig(*args, **kwargs):
717-
fig = kwargs.pop('fig', gcf())
739+
fig = _pop_or_gcf(kwargs)
718740
res = fig.savefig(*args, **kwargs)
719741
fig.canvas.draw_idle() # need this if 'transparent=True' to reset colors
720742
return res
@@ -730,7 +752,7 @@ def ginput(*args, **kwargs):
730752
731753
If *timeout* is negative, does not timeout.
732754
"""
733-
fig = kwargs.pop('fig', gcf())
755+
fig = _pop_or_gcf(kwargs)
734756
return fig.ginput(*args, **kwargs)
735757

736758

@@ -745,27 +767,27 @@ def waitforbuttonpress(*args, **kwargs):
745767
746768
If *timeout* is negative, does not timeout.
747769
"""
748-
fig = kwargs.pop('fig', gcf())
770+
fig = _pop_or_gcf(kwargs)
749771
return fig.waitforbuttonpress(*args, **kwargs)
750772

751773

752774
# Putting things in figures
753775

754776
@docstring.copy_dedent(Figure.text)
755777
def figtext(*args, **kwargs):
756-
fig = kwargs.pop('fig', gcf())
778+
fig = _pop_or_gcf(kwargs)
757779
return fig.text(*args, **kwargs)
758780

759781

760782
@docstring.copy_dedent(Figure.suptitle)
761783
def suptitle(*args, **kwargs):
762-
fig = kwargs.pop('fig', gcf())
784+
fig = _pop_or_gcf(kwargs)
763785
return fig.suptitle(*args, **kwargs)
764786

765787

766788
@docstring.copy_dedent(Figure.figimage)
767789
def figimage(*args, **kwargs):
768-
fig = kwargs.pop('fig', gcf())
790+
fig = _pop_or_gcf(kwargs)
769791
return fig.figimage(*args, **kwargs)
770792

771793

@@ -804,7 +826,7 @@ def figlegend(*args, **kwargs):
804826
:func:`~matplotlib.pyplot.legend`
805827
806828
"""
807-
fig = kwargs.pop('fig', gcf())
829+
fig = _pop_or_gcf(kwargs)
808830
return fig.legend(*args, **kwargs)
809831

810832

@@ -986,7 +1008,7 @@ def gca(**kwargs):
9861008
--------
9871009
matplotlib.figure.Figure.gca : The figure's gca method.
9881010
"""
989-
fig = kwargs.pop('fig', gcf())
1011+
fig = _pop_or_gcf(kwargs)
9901012
return fig.gca(**kwargs)
9911013

9921014
# More ways of creating axes:
@@ -1081,7 +1103,7 @@ def subplot(*args, **kwargs):
10811103
warnings.warn("The subplot index argument to subplot() appears"
10821104
" to be a boolean. Did you intend to use subplots()?")
10831105

1084-
fig = kwargs.pop('fig', gcf())
1106+
fig = _pop_or_gcf(kwargs)
10851107
a = fig.add_subplot(*args, **kwargs)
10861108
bbox = a.bbox
10871109
byebye = []
@@ -1307,7 +1329,7 @@ def subplots_adjust(*args, **kwargs):
13071329
13081330
The actual defaults are controlled by the rc file
13091331
"""
1310-
fig = kwargs.pop('fig', gcf())
1332+
fig = _pop_or_gcf(kwargs)
13111333
fig.subplots_adjust(*args, **kwargs)
13121334

13131335

@@ -1417,7 +1439,7 @@ def title(s, *args, **kwargs):
14171439
properties.
14181440
14191441
"""
1420-
return kwargs.pop('ax', gca()).set_title(s, *args, **kwargs)
1442+
return _pop_or_gca(kwargs).set_title(s, *args, **kwargs)
14211443

14221444
## Axis ##
14231445

@@ -1488,7 +1510,7 @@ def axis(*v, **kwargs):
14881510
:func:`xlim`, :func:`ylim`
14891511
For setting the x- and y-limits individually.
14901512
"""
1491-
return kwargs.pop('ax', gca()).axis(*v, **kwargs)
1513+
return _pop_or_gca(kwargs).axis(*v, **kwargs)
14921514

14931515

14941516
def xlabel(s, *args, **kwargs):
@@ -1508,7 +1530,7 @@ def xlabel(s, *args, **kwargs):
15081530
:func:`~matplotlib.pyplot.text`
15091531
For information on how override and the optional args work
15101532
"""
1511-
return kwargs.pop('ax', gca()).set_xlabel(s, *args, **kwargs)
1533+
return _pop_or_gca(kwargs).set_xlabel(s, *args, **kwargs)
15121534

15131535

15141536
def ylabel(s, *args, **kwargs):
@@ -1529,7 +1551,7 @@ def ylabel(s, *args, **kwargs):
15291551
For information on how override and the optional args
15301552
work.
15311553
"""
1532-
return kwargs.pop('ax', gca()).set_ylabel(s, *args, **kwargs)
1554+
return _pop_or_gca(kwargs).set_ylabel(s, *args, **kwargs)
15331555

15341556

15351557
def xlim(*args, **kwargs):
@@ -1553,7 +1575,7 @@ def xlim(*args, **kwargs):
15531575
The new axis limits are returned as a length 2 tuple.
15541576
15551577
"""
1556-
ax = kwargs.pop('ax', gca())
1578+
ax = _pop_or_gca(kwargs)
15571579
if not args and not kwargs:
15581580
return ax.get_xlim()
15591581
ret = ax.set_xlim(*args, **kwargs)
@@ -1580,7 +1602,7 @@ def ylim(*args, **kwargs):
15801602
15811603
The new axis limits are returned as a length 2 tuple.
15821604
"""
1583-
ax = kwargs.pop('ax', gca())
1605+
ax = _pop_or_gca(kwargs)
15841606
if not args and not kwargs:
15851607
return ax.get_ylim()
15861608
ret = ax.set_ylim(*args, **kwargs)
@@ -1602,7 +1624,7 @@ def xscale(*args, **kwargs):
16021624
16031625
%(scale_docs)s
16041626
"""
1605-
kwargs.pop('ax', gca()).set_xscale(*args, **kwargs)
1627+
_pop_or_gca(kwargs).set_xscale(*args, **kwargs)
16061628

16071629

16081630
@docstring.dedent_interpd
@@ -1620,7 +1642,7 @@ def yscale(*args, **kwargs):
16201642
16211643
%(scale_docs)s
16221644
"""
1623-
kwargs.pop('ax', gca()).set_yscale(*args, **kwargs)
1645+
_pop_or_gca(kwargs).set_yscale(*args, **kwargs)
16241646

16251647

16261648
def xticks(*args, **kwargs):
@@ -1644,7 +1666,7 @@ def xticks(*args, **kwargs):
16441666
16451667
xticks( arange(12), calendar.month_name[1:13], rotation=17 )
16461668
"""
1647-
ax = kwargs.pop('ax', gca())
1669+
ax = _pop_or_gca(kwargs)
16481670

16491671
if len(args)==0:
16501672
locs = ax.get_xticks()
@@ -1684,7 +1706,7 @@ def yticks(*args, **kwargs):
16841706
16851707
yticks( arange(12), calendar.month_name[1:13], rotation=45 )
16861708
"""
1687-
ax = kwargs.pop('ax', gca())
1709+
ax = _pop_or_gca(kwargs)
16881710

16891711
if len(args) == 0:
16901712
locs = ax.get_yticks()
@@ -1757,7 +1779,7 @@ def rgrids(*args, **kwargs):
17571779
lines, labels = rgrids( (0.25, 0.5, 1.0), ('Tom', 'Dick', 'Harry' )
17581780
17591781
"""
1760-
ax = kwargs.pop('ax', gca())
1782+
ax = _pop_or_gca(kwargs)
17611783
if not isinstance(ax, PolarAxes):
17621784
raise RuntimeError('rgrids only defined for polar axes')
17631785
if len(args)==0:
@@ -1817,7 +1839,7 @@ def thetagrids(*args, **kwargs):
18171839
# set the locations and labels of the radial gridlines and labels
18181840
lines, labels = thetagrids( range(45,360,90), ('NE', 'NW', 'SW','SE') )
18191841
"""
1820-
ax = kwargs.pop('ax', gca())
1842+
ax = _pop_or_gca(kwargs)
18211843
if not isinstance(ax, PolarAxes):
18221844
raise RuntimeError('rgrids only defined for polar axes')
18231845
if len(args)==0:
@@ -2473,9 +2495,8 @@ def _autogen_docstring(base):
24732495
# return an image or a line.
24742496
@_autogen_docstring(Axes.spy)
24752497
def spy(Z, precision=0, marker=None, markersize=None, aspect='equal',
2476-
ax=None, **kwargs):
2477-
if ax is None:
2478-
ax = gca()
2498+
**kwargs):
2499+
ax = _pop_or_gca(kwargs)
24792500
hold = kwargs.pop('hold', None)
24802501
# allow callers to override the hold state by passing hold=True|False
24812502
washold = ax._hold

0 commit comments

Comments
 (0)