Skip to content

Commit b01bdf4

Browse files
authored
Merge pull request #10545 from anntzer/kwonlyfy
Change manual kwargs popping to kwonly arguments.
2 parents b33aabc + a68784f commit b01bdf4

37 files changed

+199
-304
lines changed

examples/api/radar_chart.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,8 @@ def __init__(self, *args, **kwargs):
6464
# rotate plot such that the first axis is at the top
6565
self.set_theta_zero_location('N')
6666

67-
def fill(self, *args, **kwargs):
67+
def fill(self, *args, closed=True, **kwargs):
6868
"""Override fill so that line is closed by default"""
69-
closed = kwargs.pop('closed', True)
7069
return super().fill(closed=closed, *args, **kwargs)
7170

7271
def plot(self, *args, **kwargs):

examples/images_contours_and_fields/contour_demo.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@
9898
plt.setp(zc, linewidth=4)
9999

100100
ax.clabel(CS, levels[1::2], # label every second level
101-
inline=1, fmt='%1.1f',
102-
cmap='flag', fontsize=14)
101+
inline=1, fmt='%1.1f', fontsize=14)
103102

104103
# make a colorbar for the contour lines
105104
CB = fig.colorbar(CS, shrink=0.8, extend='both')

examples/scales/custom_scale.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class MercatorLatitudeScale(mscale.ScaleBase):
4444
# scale.
4545
name = 'mercator'
4646

47-
def __init__(self, axis, **kwargs):
47+
def __init__(self, axis, *, thresh=np.deg2rad(85), **kwargs):
4848
"""
4949
Any keyword arguments passed to ``set_xscale`` and
5050
``set_yscale`` will be passed along to the scale's
@@ -53,8 +53,7 @@ def __init__(self, axis, **kwargs):
5353
thresh: The degree above which to crop the data.
5454
"""
5555
mscale.ScaleBase.__init__(self)
56-
thresh = kwargs.pop("thresh", np.radians(85))
57-
if thresh >= np.pi / 2.0:
56+
if thresh >= np.pi / 2:
5857
raise ValueError("thresh must be less than pi/2")
5958
self.thresh = thresh
6059

examples/subplots_axes_and_figures/custom_figure_class.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212

1313

1414
class MyFigure(Figure):
15-
def __init__(self, *args, **kwargs):
15+
def __init__(self, *args, figtitle='hi mom', **kwargs):
1616
"""
1717
custom kwarg figtitle is a figure title
1818
"""
19-
figtitle = kwargs.pop('figtitle', 'hi mom')
20-
Figure.__init__(self, *args, **kwargs)
19+
super().__init__(*args, **kwargs)
2120
self.text(0.5, 0.95, figtitle, ha='center')
2221

2322

examples/text_labels_and_annotations/usetex_baseline_test.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,15 @@ class Axes(maxes.Axes):
2020
usetex=False in the same figure. It does not work in the ps backend.
2121
"""
2222

23-
def __init__(self, *kl, **kw):
24-
self.usetex = kw.pop("usetex", "False")
25-
self.preview = kw.pop("preview", "False")
26-
27-
maxes.Axes.__init__(self, *kl, **kw)
23+
def __init__(self, *args, usetex=False, preview=False, **kwargs):
24+
self.usetex = usetex
25+
self.preview = preview
26+
super().__init__(*args, **kwargs)
2827

2928
def draw(self, renderer):
30-
usetex = plt.rcParams["text.usetex"]
31-
preview = plt.rcParams["text.latex.preview"]
32-
plt.rcParams["text.usetex"] = self.usetex
33-
plt.rcParams["text.latex.preview"] = self.preview
34-
35-
maxes.Axes.draw(self, renderer)
36-
37-
plt.rcParams["text.usetex"] = usetex
38-
plt.rcParams["text.latex.preview"] = preview
29+
with plt.rc_context({"text.usetex": self.usetex,
30+
"text.latex.preview": self.preview}):
31+
super().draw(renderer)
3932

4033

4134
subplot = maxes.subplot_class_factory(Axes)

examples/user_interfaces/toolmanager_sgskip.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ class GroupHideTool(ToolToggleBase):
5656
description = 'Show by gid'
5757
default_toggled = True
5858

59-
def __init__(self, *args, **kwargs):
60-
self.gid = kwargs.pop('gid')
61-
ToolToggleBase.__init__(self, *args, **kwargs)
59+
def __init__(self, *args, gid, **kwargs):
60+
self.gid = gid
61+
super().__init__(*args, **kwargs)
6262

6363
def enable(self, *args):
6464
self.set_lines_visibility(True)

lib/matplotlib/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,7 +1703,7 @@ def param(func):
17031703
pass
17041704

17051705
@functools.wraps(func)
1706-
def inner(ax, *args, **kwargs):
1706+
def inner(ax, *args, data=None, **kwargs):
17071707
# this is needed because we want to change these values if
17081708
# arg_names_at_runtime==True, but python does not allow assigning
17091709
# to a variable in a outer scope. So use some new local ones and
@@ -1714,8 +1714,6 @@ def inner(ax, *args, **kwargs):
17141714

17151715
label = None
17161716

1717-
data = kwargs.pop('data', None)
1718-
17191717
if data is None: # data validation
17201718
args = tuple(sanitize_sequence(a) for a in args)
17211719
else:

lib/matplotlib/axes/_axes.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5173,20 +5173,14 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
51735173
return im
51745174

51755175
@staticmethod
5176-
def _pcolorargs(funcname, *args, **kw):
5177-
# This takes one kwarg, allmatch.
5178-
# If allmatch is True, then the incoming X, Y, C must
5179-
# have matching dimensions, taking into account that
5180-
# X and Y can be 1-D rather than 2-D. This perfect
5181-
# match is required for Gouroud shading. For flat
5182-
# shading, X and Y specify boundaries, so we need
5183-
# one more boundary than color in each direction.
5184-
# For convenience, and consistent with Matlab, we
5185-
# discard the last row and/or column of C if necessary
5186-
# to meet this condition. This is done if allmatch
5187-
# is False.
5188-
5189-
allmatch = kw.pop("allmatch", False)
5176+
def _pcolorargs(funcname, *args, allmatch=False):
5177+
# If allmatch is True, then the incoming X, Y, C must have matching
5178+
# dimensions, taking into account that X and Y can be 1-D rather than
5179+
# 2-D. This perfect match is required for Gouroud shading. For flat
5180+
# shading, X and Y specify boundaries, so we need one more boundary
5181+
# than color in each direction. For convenience, and consistent with
5182+
# Matlab, we discard the last row and/or column of C if necessary to
5183+
# meet this condition. This is done if allmatch is False.
51905184

51915185
if len(args) == 1:
51925186
C = np.asanyarray(args[0])
@@ -5233,7 +5227,7 @@ def _pcolorargs(funcname, *args, **kw):
52335227
'Incompatible X, Y inputs to %s; see help(%s)' % (
52345228
funcname, funcname))
52355229
if allmatch:
5236-
if not (Nx == numCols and Ny == numRows):
5230+
if (Nx, Ny) != (numCols, numRows):
52375231
raise TypeError('Dimensions of C %s are incompatible with'
52385232
' X (%d) and/or Y (%d); see help(%s)' % (
52395233
C.shape, Nx, Ny, funcname))

lib/matplotlib/axes/_base.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2690,7 +2690,8 @@ def grid(self, b=None, which='major', axis='both', **kwargs):
26902690
if axis == 'y' or axis == 'both':
26912691
self.yaxis.grid(b, which=which, **kwargs)
26922692

2693-
def ticklabel_format(self, **kwargs):
2693+
def ticklabel_format(self, *, axis='both', style='', scilimits=None,
2694+
useOffset=None, useLocale=None, useMathText=None):
26942695
"""
26952696
Change the `~matplotlib.ticker.ScalarFormatter` used by
26962697
default for linear axes.
@@ -2700,6 +2701,7 @@ def ticklabel_format(self, **kwargs):
27002701
============== =========================================
27012702
Keyword Description
27022703
============== =========================================
2704+
*axis* [ 'x' | 'y' | 'both' ]
27032705
*style* [ 'sci' (or 'scientific') | 'plain' ]
27042706
plain turns off scientific notation
27052707
*scilimits* (m, n), pair of integers; if *style*
@@ -2712,7 +2714,6 @@ def ticklabel_format(self, **kwargs):
27122714
if False, no offset will be used; if a
27132715
numeric offset is specified, it will be
27142716
used.
2715-
*axis* [ 'x' | 'y' | 'both' ]
27162717
*useLocale* If True, format the number according to
27172718
the current locale. This affects things
27182719
such as the character used for the
@@ -2731,12 +2732,8 @@ def ticklabel_format(self, **kwargs):
27312732
:exc:`AttributeError` will be raised.
27322733
27332734
"""
2734-
style = kwargs.pop('style', '').lower()
2735-
scilimits = kwargs.pop('scilimits', None)
2736-
useOffset = kwargs.pop('useOffset', None)
2737-
useLocale = kwargs.pop('useLocale', None)
2738-
useMathText = kwargs.pop('useMathText', None)
2739-
axis = kwargs.pop('axis', 'both').lower()
2735+
style = style.lower()
2736+
axis = axis.lower()
27402737
if scilimits is not None:
27412738
try:
27422739
m, n = scilimits

lib/matplotlib/axis.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,7 +1616,7 @@ def set_pickradius(self, pickradius):
16161616
"""
16171617
self.pickradius = pickradius
16181618

1619-
def set_ticklabels(self, ticklabels, *args, **kwargs):
1619+
def set_ticklabels(self, ticklabels, *args, minor=False, **kwargs):
16201620
"""
16211621
Set the text values of the tick labels. Return a list of Text
16221622
instances. Use *kwarg* *minor=True* to select minor ticks.
@@ -1645,7 +1645,6 @@ def set_ticklabels(self, ticklabels, *args, **kwargs):
16451645
# replace the ticklabels list with the processed one
16461646
ticklabels = get_labels
16471647

1648-
minor = kwargs.pop('minor', False)
16491648
if minor:
16501649
self.set_minor_formatter(mticker.FixedFormatter(ticklabels))
16511650
ticks = self.get_minor_ticks()

lib/matplotlib/backends/backend_agg.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ def print_to_buffer(self):
516516

517517
if _has_pil:
518518
# add JPEG support
519-
def print_jpg(self, filename_or_obj, *args, **kwargs):
519+
def print_jpg(self, filename_or_obj, *args, dryrun=False, **kwargs):
520520
"""
521521
Other Parameters
522522
----------------
@@ -536,7 +536,7 @@ def print_jpg(self, filename_or_obj, *args, **kwargs):
536536
should be stored as a progressive JPEG file.
537537
"""
538538
buf, size = self.print_to_buffer()
539-
if kwargs.pop("dryrun", False):
539+
if dryrun:
540540
return
541541
# The image is "pasted" onto a white background image to safely
542542
# handle any transparency
@@ -557,9 +557,9 @@ def print_jpg(self, filename_or_obj, *args, **kwargs):
557557
print_jpeg = print_jpg
558558

559559
# add TIFF support
560-
def print_tif(self, filename_or_obj, *args, **kwargs):
560+
def print_tif(self, filename_or_obj, *args, dryrun=False, **kwargs):
561561
buf, size = self.print_to_buffer()
562-
if kwargs.pop("dryrun", False):
562+
if dryrun:
563563
return
564564
image = Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1)
565565
dpi = (self.figure.dpi, self.figure.dpi)

lib/matplotlib/backends/backend_pdf.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2555,21 +2555,22 @@ def draw(self):
25552555
def get_default_filetype(self):
25562556
return 'pdf'
25572557

2558-
def print_pdf(self, filename, **kwargs):
2559-
image_dpi = kwargs.get('dpi', 72) # dpi to use for images
2558+
def print_pdf(self, filename, *,
2559+
dpi=72, # dpi to use for images
2560+
bbox_inches_restore=None, metadata=None,
2561+
**kwargs):
25602562
self.figure.set_dpi(72) # there are 72 pdf points to an inch
25612563
width, height = self.figure.get_size_inches()
25622564
if isinstance(filename, PdfPages):
25632565
file = filename._file
25642566
else:
2565-
file = PdfFile(filename, metadata=kwargs.pop("metadata", None))
2567+
file = PdfFile(filename, metadata=metadata)
25662568
try:
25672569
file.newPage(width, height)
2568-
_bbox_inches_restore = kwargs.pop("bbox_inches_restore", None)
25692570
renderer = MixedModeRenderer(
2570-
self.figure, width, height, image_dpi,
2571-
RendererPdf(file, image_dpi, height, width),
2572-
bbox_inches_restore=_bbox_inches_restore)
2571+
self.figure, width, height, dpi,
2572+
RendererPdf(file, dpi, height, width),
2573+
bbox_inches_restore=bbox_inches_restore)
25732574
self.figure.draw(renderer)
25742575
renderer.finalize()
25752576
if not isinstance(filename, PdfPages):

lib/matplotlib/backends/backend_pgf.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -802,8 +802,9 @@ class FigureCanvasPgf(FigureCanvasBase):
802802
def get_default_filetype(self):
803803
return 'pdf'
804804

805-
def _print_pgf_to_fh(self, fh, *args, **kwargs):
806-
if kwargs.get("dryrun", False):
805+
def _print_pgf_to_fh(self, fh, *args,
806+
dryrun=False, bbox_inches_restore=None, **kwargs):
807+
if dryrun:
807808
renderer = RendererPgf(self.figure, None, dummy=True)
808809
self.figure.draw(renderer)
809810
return
@@ -849,10 +850,9 @@ def _print_pgf_to_fh(self, fh, *args, **kwargs):
849850
r"\pgfpathrectangle{\pgfpointorigin}{\pgfqpoint{%fin}{%fin}}"
850851
% (w, h))
851852
writeln(fh, r"\pgfusepath{use as bounding box, clip}")
852-
_bbox_inches_restore = kwargs.pop("bbox_inches_restore", None)
853853
renderer = MixedModeRenderer(self.figure, w, h, dpi,
854854
RendererPgf(self.figure, fh),
855-
bbox_inches_restore=_bbox_inches_restore)
855+
bbox_inches_restore=bbox_inches_restore)
856856
self.figure.draw(renderer)
857857

858858
# end the pgfpicture environment

lib/matplotlib/backends/backend_ps.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -959,9 +959,11 @@ def _print_ps(self, outfile, format, *args, **kwargs):
959959
orientation, isLandscape, papertype,
960960
**kwargs)
961961

962-
def _print_figure(self, outfile, format, dpi=72, facecolor='w', edgecolor='w',
963-
orientation='portrait', isLandscape=False, papertype=None,
964-
metadata=None, **kwargs):
962+
def _print_figure(
963+
self, outfile, format, dpi=72, facecolor='w', edgecolor='w',
964+
orientation='portrait', isLandscape=False, papertype=None,
965+
metadata=None, *,
966+
dryrun=False, bbox_inches_restore=None, **kwargs):
965967
"""
966968
Render the figure to hardcopy. Set the figure patch face and
967969
edge colors. This is useful because some of the GUIs have a
@@ -1031,8 +1033,6 @@ def _print_figure(self, outfile, format, dpi=72, facecolor='w', edgecolor='w',
10311033
self.figure.set_facecolor(facecolor)
10321034
self.figure.set_edgecolor(edgecolor)
10331035

1034-
1035-
dryrun = kwargs.get("dryrun", False)
10361036
if dryrun:
10371037
class NullWriter(object):
10381038
def write(self, *kl, **kwargs):
@@ -1042,14 +1042,12 @@ def write(self, *kl, **kwargs):
10421042
else:
10431043
self._pswriter = io.StringIO()
10441044

1045-
10461045
# mixed mode rendering
1047-
_bbox_inches_restore = kwargs.pop("bbox_inches_restore", None)
10481046
ps_renderer = self._renderer_class(width, height, self._pswriter,
10491047
imagedpi=dpi)
10501048
renderer = MixedModeRenderer(self.figure,
10511049
width, height, dpi, ps_renderer,
1052-
bbox_inches_restore=_bbox_inches_restore)
1050+
bbox_inches_restore=bbox_inches_restore)
10531051

10541052
self.figure.draw(renderer)
10551053

@@ -1199,9 +1197,10 @@ def do_nothing():
11991197
with io.open(outfile, 'w', encoding='latin-1') as fh:
12001198
print_figure_impl(fh)
12011199

1202-
def _print_figure_tex(self, outfile, format, dpi, facecolor, edgecolor,
1203-
orientation, isLandscape, papertype, metadata=None,
1204-
**kwargs):
1200+
def _print_figure_tex(
1201+
self, outfile, format, dpi, facecolor, edgecolor,
1202+
orientation, isLandscape, papertype, metadata=None, *,
1203+
dryrun=False, bbox_inches_restore=None, **kwargs):
12051204
"""
12061205
If text.usetex is True in rc, a temporary pair of tex/eps files
12071206
are created to allow tex to manage the text layout via the PSFrags
@@ -1236,7 +1235,6 @@ def _print_figure_tex(self, outfile, format, dpi, facecolor, edgecolor,
12361235
self.figure.set_facecolor(facecolor)
12371236
self.figure.set_edgecolor(edgecolor)
12381237

1239-
dryrun = kwargs.get("dryrun", False)
12401238
if dryrun:
12411239
class NullWriter(object):
12421240
def write(self, *kl, **kwargs):
@@ -1247,12 +1245,11 @@ def write(self, *kl, **kwargs):
12471245
self._pswriter = io.StringIO()
12481246

12491247
# mixed mode rendering
1250-
_bbox_inches_restore = kwargs.pop("bbox_inches_restore", None)
12511248
ps_renderer = self._renderer_class(width, height,
12521249
self._pswriter, imagedpi=dpi)
12531250
renderer = MixedModeRenderer(self.figure,
12541251
width, height, dpi, ps_renderer,
1255-
bbox_inches_restore=_bbox_inches_restore)
1252+
bbox_inches_restore=bbox_inches_restore)
12561253

12571254
self.figure.draw(renderer)
12581255

lib/matplotlib/backends/backend_svg.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,17 +1215,16 @@ def print_svgz(self, filename, *args, **kwargs):
12151215
gzip.GzipFile(mode='w', fileobj=fh) as gzipwriter:
12161216
return self.print_svg(gzipwriter)
12171217

1218-
def _print_svg(self, filename, fh, **kwargs):
1219-
image_dpi = kwargs.pop("dpi", 72)
1218+
def _print_svg(
1219+
self, filename, fh, *, dpi=72, bbox_inches_restore=None, **kwargs):
12201220
self.figure.set_dpi(72.0)
12211221
width, height = self.figure.get_size_inches()
12221222
w, h = width * 72, height * 72
12231223

1224-
_bbox_inches_restore = kwargs.pop("bbox_inches_restore", None)
12251224
renderer = MixedModeRenderer(
1226-
self.figure, width, height, image_dpi,
1227-
RendererSVG(w, h, fh, filename, image_dpi),
1228-
bbox_inches_restore=_bbox_inches_restore)
1225+
self.figure, width, height, dpi,
1226+
RendererSVG(w, h, fh, filename, dpi),
1227+
bbox_inches_restore=bbox_inches_restore)
12291228

12301229
self.figure.draw(renderer)
12311230
renderer.finalize()

0 commit comments

Comments
 (0)