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

Skip to content

Commit b08714c

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

File tree

1 file changed

+44
-25
lines changed

1 file changed

+44
-25
lines changed

lib/matplotlib/pyplot.py

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -712,9 +712,29 @@ def draw(fig=None):
712712
fig.canvas.draw_idle()
713713

714714

715+
def _pop_or_gca(kwargs):
716+
'''Internal helper function for getting an optional ax kwarg or gca
717+
718+
.. warning::
719+
720+
This mutates the input in place!
721+
'''
722+
return kwargs.pop('ax', None) or gca()
723+
724+
725+
def _pop_or_gcf(kwargs):
726+
'''Internal helper function for getting an optional fig kwarg or gcf
727+
728+
.. warning::
729+
730+
This mutates the input in place!
731+
'''
732+
return kwargs.pop('fig', None) or gcf()
733+
734+
715735
@docstring.copy_dedent(Figure.savefig)
716736
def savefig(*args, **kwargs):
717-
fig = kwargs.pop('fig', gcf())
737+
fig = _pop_or_gcf(kwargs)
718738
res = fig.savefig(*args, **kwargs)
719739
fig.canvas.draw_idle() # need this if 'transparent=True' to reset colors
720740
return res
@@ -730,7 +750,7 @@ def ginput(*args, **kwargs):
730750
731751
If *timeout* is negative, does not timeout.
732752
"""
733-
fig = kwargs.pop('fig', gcf())
753+
fig = _pop_or_gcf(kwargs)
734754
return fig.ginput(*args, **kwargs)
735755

736756

@@ -745,27 +765,27 @@ def waitforbuttonpress(*args, **kwargs):
745765
746766
If *timeout* is negative, does not timeout.
747767
"""
748-
fig = kwargs.pop('fig', gcf())
768+
fig = _pop_or_gcf(kwargs)
749769
return fig.waitforbuttonpress(*args, **kwargs)
750770

751771

752772
# Putting things in figures
753773

754774
@docstring.copy_dedent(Figure.text)
755775
def figtext(*args, **kwargs):
756-
fig = kwargs.pop('fig', gcf())
776+
fig = _pop_or_gcf(kwargs)
757777
return fig.text(*args, **kwargs)
758778

759779

760780
@docstring.copy_dedent(Figure.suptitle)
761781
def suptitle(*args, **kwargs):
762-
fig = kwargs.pop('fig', gcf())
782+
fig = _pop_or_gcf(kwargs)
763783
return fig.suptitle(*args, **kwargs)
764784

765785

766786
@docstring.copy_dedent(Figure.figimage)
767787
def figimage(*args, **kwargs):
768-
fig = kwargs.pop('fig', gcf())
788+
fig = _pop_or_gcf(kwargs)
769789
return fig.figimage(*args, **kwargs)
770790

771791

@@ -804,7 +824,7 @@ def figlegend(*args, **kwargs):
804824
:func:`~matplotlib.pyplot.legend`
805825
806826
"""
807-
fig = kwargs.pop('fig', gcf())
827+
fig = _pop_or_gcf(kwargs)
808828
return fig.legend(*args, **kwargs)
809829

810830

@@ -986,7 +1006,7 @@ def gca(**kwargs):
9861006
--------
9871007
matplotlib.figure.Figure.gca : The figure's gca method.
9881008
"""
989-
fig = kwargs.pop('fig', gcf())
1009+
fig = _pop_or_gcf(kwargs)
9901010
return fig.gca(**kwargs)
9911011

9921012
# More ways of creating axes:
@@ -1081,7 +1101,7 @@ def subplot(*args, **kwargs):
10811101
warnings.warn("The subplot index argument to subplot() appears"
10821102
" to be a boolean. Did you intend to use subplots()?")
10831103

1084-
fig = kwargs.pop('fig', gcf())
1104+
fig = _pop_or_gcf(kwargs)
10851105
a = fig.add_subplot(*args, **kwargs)
10861106
bbox = a.bbox
10871107
byebye = []
@@ -1307,7 +1327,7 @@ def subplots_adjust(*args, **kwargs):
13071327
13081328
The actual defaults are controlled by the rc file
13091329
"""
1310-
fig = kwargs.pop('fig', gcf())
1330+
fig = _pop_or_gcf(kwargs)
13111331
fig.subplots_adjust(*args, **kwargs)
13121332

13131333

@@ -1417,7 +1437,7 @@ def title(s, *args, **kwargs):
14171437
properties.
14181438
14191439
"""
1420-
return kwargs.pop('ax', gca()).set_title(s, *args, **kwargs)
1440+
return _pop_or_gca(kwargs).set_title(s, *args, **kwargs)
14211441

14221442
## Axis ##
14231443

@@ -1488,7 +1508,7 @@ def axis(*v, **kwargs):
14881508
:func:`xlim`, :func:`ylim`
14891509
For setting the x- and y-limits individually.
14901510
"""
1491-
return kwargs.pop('ax', gca()).axis(*v, **kwargs)
1511+
return _pop_or_gca(kwargs).axis(*v, **kwargs)
14921512

14931513

14941514
def xlabel(s, *args, **kwargs):
@@ -1508,7 +1528,7 @@ def xlabel(s, *args, **kwargs):
15081528
:func:`~matplotlib.pyplot.text`
15091529
For information on how override and the optional args work
15101530
"""
1511-
return kwargs.pop('ax', gca()).set_xlabel(s, *args, **kwargs)
1531+
return _pop_or_gca(kwargs).set_xlabel(s, *args, **kwargs)
15121532

15131533

15141534
def ylabel(s, *args, **kwargs):
@@ -1529,7 +1549,7 @@ def ylabel(s, *args, **kwargs):
15291549
For information on how override and the optional args
15301550
work.
15311551
"""
1532-
return kwargs.pop('ax', gca()).set_ylabel(s, *args, **kwargs)
1552+
return _pop_or_gca(kwargs).set_ylabel(s, *args, **kwargs)
15331553

15341554

15351555
def xlim(*args, **kwargs):
@@ -1553,7 +1573,7 @@ def xlim(*args, **kwargs):
15531573
The new axis limits are returned as a length 2 tuple.
15541574
15551575
"""
1556-
ax = kwargs.pop('ax', gca())
1576+
ax = _pop_or_gca(kwargs)
15571577
if not args and not kwargs:
15581578
return ax.get_xlim()
15591579
ret = ax.set_xlim(*args, **kwargs)
@@ -1580,7 +1600,7 @@ def ylim(*args, **kwargs):
15801600
15811601
The new axis limits are returned as a length 2 tuple.
15821602
"""
1583-
ax = kwargs.pop('ax', gca())
1603+
ax = _pop_or_gca(kwargs)
15841604
if not args and not kwargs:
15851605
return ax.get_ylim()
15861606
ret = ax.set_ylim(*args, **kwargs)
@@ -1602,7 +1622,7 @@ def xscale(*args, **kwargs):
16021622
16031623
%(scale_docs)s
16041624
"""
1605-
kwargs.pop('ax', gca()).set_xscale(*args, **kwargs)
1625+
_pop_or_gca(kwargs).set_xscale(*args, **kwargs)
16061626

16071627

16081628
@docstring.dedent_interpd
@@ -1620,7 +1640,7 @@ def yscale(*args, **kwargs):
16201640
16211641
%(scale_docs)s
16221642
"""
1623-
kwargs.pop('ax', gca()).set_yscale(*args, **kwargs)
1643+
_pop_or_gca(kwargs).set_yscale(*args, **kwargs)
16241644

16251645

16261646
def xticks(*args, **kwargs):
@@ -1644,7 +1664,7 @@ def xticks(*args, **kwargs):
16441664
16451665
xticks( arange(12), calendar.month_name[1:13], rotation=17 )
16461666
"""
1647-
ax = kwargs.pop('ax', gca())
1667+
ax = _pop_or_gca(kwargs)
16481668

16491669
if len(args)==0:
16501670
locs = ax.get_xticks()
@@ -1684,7 +1704,7 @@ def yticks(*args, **kwargs):
16841704
16851705
yticks( arange(12), calendar.month_name[1:13], rotation=45 )
16861706
"""
1687-
ax = kwargs.pop('ax', gca())
1707+
ax = _pop_or_gca(kwargs)
16881708

16891709
if len(args) == 0:
16901710
locs = ax.get_yticks()
@@ -1757,7 +1777,7 @@ def rgrids(*args, **kwargs):
17571777
lines, labels = rgrids( (0.25, 0.5, 1.0), ('Tom', 'Dick', 'Harry' )
17581778
17591779
"""
1760-
ax = kwargs.pop('ax', gca())
1780+
ax = _pop_or_gca(kwargs)
17611781
if not isinstance(ax, PolarAxes):
17621782
raise RuntimeError('rgrids only defined for polar axes')
17631783
if len(args)==0:
@@ -1817,7 +1837,7 @@ def thetagrids(*args, **kwargs):
18171837
# set the locations and labels of the radial gridlines and labels
18181838
lines, labels = thetagrids( range(45,360,90), ('NE', 'NW', 'SW','SE') )
18191839
"""
1820-
ax = kwargs.pop('ax', gca())
1840+
ax = _pop_or_gca(kwargs)
18211841
if not isinstance(ax, PolarAxes):
18221842
raise RuntimeError('rgrids only defined for polar axes')
18231843
if len(args)==0:
@@ -2473,9 +2493,8 @@ def _autogen_docstring(base):
24732493
# return an image or a line.
24742494
@_autogen_docstring(Axes.spy)
24752495
def spy(Z, precision=0, marker=None, markersize=None, aspect='equal',
2476-
ax=None, **kwargs):
2477-
if ax is None:
2478-
ax = gca()
2496+
**kwargs):
2497+
ax = _pop_or_gca(kwargs)
24792498
hold = kwargs.pop('hold', None)
24802499
# allow callers to override the hold state by passing hold=True|False
24812500
washold = ax._hold

0 commit comments

Comments
 (0)