@@ -712,9 +712,29 @@ def draw(fig=None):
712
712
fig .canvas .draw_idle ()
713
713
714
714
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
+
715
735
@docstring .copy_dedent (Figure .savefig )
716
736
def savefig (* args , ** kwargs ):
717
- fig = kwargs . pop ( 'fig' , gcf () )
737
+ fig = _pop_or_gcf ( kwargs )
718
738
res = fig .savefig (* args , ** kwargs )
719
739
fig .canvas .draw_idle () # need this if 'transparent=True' to reset colors
720
740
return res
@@ -730,7 +750,7 @@ def ginput(*args, **kwargs):
730
750
731
751
If *timeout* is negative, does not timeout.
732
752
"""
733
- fig = kwargs . pop ( 'fig' , gcf () )
753
+ fig = _pop_or_gcf ( kwargs )
734
754
return fig .ginput (* args , ** kwargs )
735
755
736
756
@@ -745,27 +765,27 @@ def waitforbuttonpress(*args, **kwargs):
745
765
746
766
If *timeout* is negative, does not timeout.
747
767
"""
748
- fig = kwargs . pop ( 'fig' , gcf () )
768
+ fig = _pop_or_gcf ( kwargs )
749
769
return fig .waitforbuttonpress (* args , ** kwargs )
750
770
751
771
752
772
# Putting things in figures
753
773
754
774
@docstring .copy_dedent (Figure .text )
755
775
def figtext (* args , ** kwargs ):
756
- fig = kwargs . pop ( 'fig' , gcf () )
776
+ fig = _pop_or_gcf ( kwargs )
757
777
return fig .text (* args , ** kwargs )
758
778
759
779
760
780
@docstring .copy_dedent (Figure .suptitle )
761
781
def suptitle (* args , ** kwargs ):
762
- fig = kwargs . pop ( 'fig' , gcf () )
782
+ fig = _pop_or_gcf ( kwargs )
763
783
return fig .suptitle (* args , ** kwargs )
764
784
765
785
766
786
@docstring .copy_dedent (Figure .figimage )
767
787
def figimage (* args , ** kwargs ):
768
- fig = kwargs . pop ( 'fig' , gcf () )
788
+ fig = _pop_or_gcf ( kwargs )
769
789
return fig .figimage (* args , ** kwargs )
770
790
771
791
@@ -804,7 +824,7 @@ def figlegend(*args, **kwargs):
804
824
:func:`~matplotlib.pyplot.legend`
805
825
806
826
"""
807
- fig = kwargs . pop ( 'fig' , gcf () )
827
+ fig = _pop_or_gcf ( kwargs )
808
828
return fig .legend (* args , ** kwargs )
809
829
810
830
@@ -986,7 +1006,7 @@ def gca(**kwargs):
986
1006
--------
987
1007
matplotlib.figure.Figure.gca : The figure's gca method.
988
1008
"""
989
- fig = kwargs . pop ( 'fig' , gcf () )
1009
+ fig = _pop_or_gcf ( kwargs )
990
1010
return fig .gca (** kwargs )
991
1011
992
1012
# More ways of creating axes:
@@ -1081,7 +1101,7 @@ def subplot(*args, **kwargs):
1081
1101
warnings .warn ("The subplot index argument to subplot() appears"
1082
1102
" to be a boolean. Did you intend to use subplots()?" )
1083
1103
1084
- fig = kwargs . pop ( 'fig' , gcf () )
1104
+ fig = _pop_or_gcf ( kwargs )
1085
1105
a = fig .add_subplot (* args , ** kwargs )
1086
1106
bbox = a .bbox
1087
1107
byebye = []
@@ -1307,7 +1327,7 @@ def subplots_adjust(*args, **kwargs):
1307
1327
1308
1328
The actual defaults are controlled by the rc file
1309
1329
"""
1310
- fig = kwargs . pop ( 'fig' , gcf () )
1330
+ fig = _pop_or_gcf ( kwargs )
1311
1331
fig .subplots_adjust (* args , ** kwargs )
1312
1332
1313
1333
@@ -1417,7 +1437,7 @@ def title(s, *args, **kwargs):
1417
1437
properties.
1418
1438
1419
1439
"""
1420
- return kwargs . pop ( 'ax' , gca () ).set_title (s , * args , ** kwargs )
1440
+ return _pop_or_gca ( kwargs ).set_title (s , * args , ** kwargs )
1421
1441
1422
1442
## Axis ##
1423
1443
@@ -1488,7 +1508,7 @@ def axis(*v, **kwargs):
1488
1508
:func:`xlim`, :func:`ylim`
1489
1509
For setting the x- and y-limits individually.
1490
1510
"""
1491
- return kwargs . pop ( 'ax' , gca () ).axis (* v , ** kwargs )
1511
+ return _pop_or_gca ( kwargs ).axis (* v , ** kwargs )
1492
1512
1493
1513
1494
1514
def xlabel (s , * args , ** kwargs ):
@@ -1508,7 +1528,7 @@ def xlabel(s, *args, **kwargs):
1508
1528
:func:`~matplotlib.pyplot.text`
1509
1529
For information on how override and the optional args work
1510
1530
"""
1511
- return kwargs . pop ( 'ax' , gca () ).set_xlabel (s , * args , ** kwargs )
1531
+ return _pop_or_gca ( kwargs ).set_xlabel (s , * args , ** kwargs )
1512
1532
1513
1533
1514
1534
def ylabel (s , * args , ** kwargs ):
@@ -1529,7 +1549,7 @@ def ylabel(s, *args, **kwargs):
1529
1549
For information on how override and the optional args
1530
1550
work.
1531
1551
"""
1532
- return kwargs . pop ( 'ax' , gca () ).set_ylabel (s , * args , ** kwargs )
1552
+ return _pop_or_gca ( kwargs ).set_ylabel (s , * args , ** kwargs )
1533
1553
1534
1554
1535
1555
def xlim (* args , ** kwargs ):
@@ -1553,7 +1573,7 @@ def xlim(*args, **kwargs):
1553
1573
The new axis limits are returned as a length 2 tuple.
1554
1574
1555
1575
"""
1556
- ax = kwargs . pop ( 'ax' , gca () )
1576
+ ax = _pop_or_gca ( kwargs )
1557
1577
if not args and not kwargs :
1558
1578
return ax .get_xlim ()
1559
1579
ret = ax .set_xlim (* args , ** kwargs )
@@ -1580,7 +1600,7 @@ def ylim(*args, **kwargs):
1580
1600
1581
1601
The new axis limits are returned as a length 2 tuple.
1582
1602
"""
1583
- ax = kwargs . pop ( 'ax' , gca () )
1603
+ ax = _pop_or_gca ( kwargs )
1584
1604
if not args and not kwargs :
1585
1605
return ax .get_ylim ()
1586
1606
ret = ax .set_ylim (* args , ** kwargs )
@@ -1602,7 +1622,7 @@ def xscale(*args, **kwargs):
1602
1622
1603
1623
%(scale_docs)s
1604
1624
"""
1605
- kwargs . pop ( 'ax' , gca () ).set_xscale (* args , ** kwargs )
1625
+ _pop_or_gca ( kwargs ).set_xscale (* args , ** kwargs )
1606
1626
1607
1627
1608
1628
@docstring .dedent_interpd
@@ -1620,7 +1640,7 @@ def yscale(*args, **kwargs):
1620
1640
1621
1641
%(scale_docs)s
1622
1642
"""
1623
- kwargs . pop ( 'ax' , gca () ).set_yscale (* args , ** kwargs )
1643
+ _pop_or_gca ( kwargs ).set_yscale (* args , ** kwargs )
1624
1644
1625
1645
1626
1646
def xticks (* args , ** kwargs ):
@@ -1644,7 +1664,7 @@ def xticks(*args, **kwargs):
1644
1664
1645
1665
xticks( arange(12), calendar.month_name[1:13], rotation=17 )
1646
1666
"""
1647
- ax = kwargs . pop ( 'ax' , gca () )
1667
+ ax = _pop_or_gca ( kwargs )
1648
1668
1649
1669
if len (args )== 0 :
1650
1670
locs = ax .get_xticks ()
@@ -1684,7 +1704,7 @@ def yticks(*args, **kwargs):
1684
1704
1685
1705
yticks( arange(12), calendar.month_name[1:13], rotation=45 )
1686
1706
"""
1687
- ax = kwargs . pop ( 'ax' , gca () )
1707
+ ax = _pop_or_gca ( kwargs )
1688
1708
1689
1709
if len (args ) == 0 :
1690
1710
locs = ax .get_yticks ()
@@ -1757,7 +1777,7 @@ def rgrids(*args, **kwargs):
1757
1777
lines, labels = rgrids( (0.25, 0.5, 1.0), ('Tom', 'Dick', 'Harry' )
1758
1778
1759
1779
"""
1760
- ax = kwargs . pop ( 'ax' , gca () )
1780
+ ax = _pop_or_gca ( kwargs )
1761
1781
if not isinstance (ax , PolarAxes ):
1762
1782
raise RuntimeError ('rgrids only defined for polar axes' )
1763
1783
if len (args )== 0 :
@@ -1817,7 +1837,7 @@ def thetagrids(*args, **kwargs):
1817
1837
# set the locations and labels of the radial gridlines and labels
1818
1838
lines, labels = thetagrids( range(45,360,90), ('NE', 'NW', 'SW','SE') )
1819
1839
"""
1820
- ax = kwargs . pop ( 'ax' , gca () )
1840
+ ax = _pop_or_gca ( kwargs )
1821
1841
if not isinstance (ax , PolarAxes ):
1822
1842
raise RuntimeError ('rgrids only defined for polar axes' )
1823
1843
if len (args )== 0 :
@@ -2473,9 +2493,8 @@ def _autogen_docstring(base):
2473
2493
# return an image or a line.
2474
2494
@_autogen_docstring (Axes .spy )
2475
2495
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 )
2479
2498
hold = kwargs .pop ('hold' , None )
2480
2499
# allow callers to override the hold state by passing hold=True|False
2481
2500
washold = ax ._hold
0 commit comments