Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf26efd9

Browse files
committed
MNT: defer calling gca or gcf unless we absolutely must
1 parent27540bb commitf26efd9

File tree

1 file changed

+46
-25
lines changed

1 file changed

+46
-25
lines changed

‎lib/matplotlib/pyplot.py

Lines changed: 46 additions & 25 deletions
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+
returnkwargs.pop('ax',None)orgca()
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+
returnkwargs.pop('fig',None)orgcf()
735+
736+
715737
@docstring.copy_dedent(Figure.savefig)
716738
defsavefig(*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
returnres
@@ -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
returnfig.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
returnfig.waitforbuttonpress(*args,**kwargs)
750772

751773

752774
# Putting things in figures
753775

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

759781

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

765787

766788
@docstring.copy_dedent(Figure.figimage)
767789
deffigimage(*args,**kwargs):
768-
fig=kwargs.pop('fig',gcf())
790+
fig=_pop_or_gcf(kwargs)
769791
returnfig.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
returnfig.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
returnfig.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-
returnkwargs.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-
returnkwargs.pop('ax',gca()).axis(*v,**kwargs)
1513+
return_pop_or_gca(kwargs).axis(*v,**kwargs)
14921514

14931515

14941516
defxlabel(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-
returnkwargs.pop('ax',gca()).set_xlabel(s,*args,**kwargs)
1533+
return_pop_or_gca(kwargs).set_xlabel(s,*args,**kwargs)
15121534

15131535

15141536
defylabel(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-
returnkwargs.pop('ax',gca()).set_ylabel(s,*args,**kwargs)
1554+
return_pop_or_gca(kwargs).set_ylabel(s,*args,**kwargs)
15331555

15341556

15351557
defxlim(*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
ifnotargsandnotkwargs:
15581580
returnax.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
ifnotargsandnotkwargs:
15851607
returnax.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
defxticks(*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
iflen(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
iflen(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
ifnotisinstance(ax,PolarAxes):
17621784
raiseRuntimeError('rgrids only defined for polar axes')
17631785
iflen(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
ifnotisinstance(ax,PolarAxes):
18221844
raiseRuntimeError('rgrids only defined for polar axes')
18231845
iflen(args)==0:
@@ -2473,9 +2495,8 @@ def _autogen_docstring(base):
24732495
# return an image or a line.
24742496
@_autogen_docstring(Axes.spy)
24752497
defspy(Z,precision=0,marker=None,markersize=None,aspect='equal',
2476-
ax=None,**kwargs):
2477-
ifaxisNone:
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)

[8]ページ先頭

©2009-2025 Movatter.jp