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