8000 Use more kwonly arguments, less manual kwargs-popping. · matplotlib/matplotlib@8702500 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8702500

Browse files
committed
Use more kwonly arguments, less manual kwargs-popping.
1 parent fc413b3 commit 8702500

File tree

8 files changed

+43
-63
lines changed

8 files changed

+43
-63
lines changed

lib/matplotlib/artist.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,7 +1527,7 @@ def getp(obj, property=None):
15271527
get = getp
15281528

15291529

1530-
def setp(obj, *args, **kwargs):
1530+
def setp(obj, *args, file=None, **kwargs):
15311531
"""
15321532
Set a property on an artist object.
15331533
@@ -1551,8 +1551,8 @@ def setp(obj, *args, **kwargs):
15511551
>>> setp(line)
15521552
... long output listing omitted
15531553
1554-
You may specify another output file to `setp` if `sys.stdout` is not
1555-
acceptable for some reason using the *file* keyword-only argument::
1554+
By default `setp` prints to `sys.stdout`, but this can be modified using
1555+
the *file* keyword-only argument::
15561556
15571557
>>> with fopen('output.log') as f:
15581558
>>> setp(line, file=f)
@@ -1587,16 +1587,11 @@ def setp(obj, *args, **kwargs):
15871587

15881588
insp = ArtistInspector(objs[0])
15891589

1590-
# file has to be popped before checking if kwargs is empty
1591-
printArgs = {}
1592-
if 'file' in kwargs:
1593-
printArgs['file'] = kwargs.pop('file')
1594-
15951590
if not kwargs and len(args) < 2:
15961591
if args:
1597-
print(insp.pprint_setters(prop=args[0]), **printArgs)
1592+
print(insp.pprint_setters(prop=args[0]), file=file)
15981593
else:
1599-
print('\n'.join(insp.pprint_setters()), **printArgs)
1594+
print('\n'.join(insp.pprint_setters()), file=file)
16001595
return
16011596

16021597
if len(args) % 2:

lib/matplotlib/axes/_axes.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,7 @@ def legend(self, *args, **kwargs):
404404
def _remove_legend(self, legend):
405405
self.legend_ = None
406406

407-
def inset_axes(self, bounds, *, transform=None, zorder=5,
408-
**kwargs):
407+
def inset_axes(self, bounds, *, transform=None, zorder=5, **kwargs):
409408
"""
410409
Add a child inset axes to this existing axes.
411410
@@ -428,7 +427,7 @@ def inset_axes(self, bounds, *, transform=None, zorder=5,
428427
parent axes.
429428
430429
**kwargs
431-
Other keyword arguments are passed on to the `.Axes` child axes.
430+
Other keyword arguments are passed on to the child `.Axes`.
432431
433432
Returns
434433
-------
@@ -449,14 +448,13 @@ def inset_axes(self, bounds, *, transform=None, zorder=5,
449448
"""
450449
if transform is None:
451450
transform = self.transAxes
452-
label = kwargs.pop('label', 'inset_axes')
451+
kwargs.setdefault('label', 'inset_axes')
453452

454453
# This puts the rectangle into figure-relative coordinates.
455454
inset_locator = _make_inset_locator(bounds, transform, self)
456455
bb = inset_locator(None, None)
457456

458-
inset_ax = Axes(self.figure, bb.bounds, zorder=zorder,
459-
label=label, **kwargs)
457+
inset_ax = Axes(self.figure, bb.bounds, zorder=zorder, **kwargs)
460458

461459
# this locator lets the axes move if in data coordinates.
462460
# it gets called in `ax.apply_aspect() (of all places)
@@ -467,8 +465,8 @@ def inset_axes(self, bounds, *, transform=None, zorder=5,
467465
return inset_ax
468466

469467
def indicate_inset(self, bounds, inset_ax=None, *, transform=None,
470-
facecolor='none', edgecolor='0.5', alpha=0.5,
471-
zorder=4.99, **kwargs):
468+
facecolor='none', edgecolor='0.5', alpha=0.5,
469+
zorder=4.99, **kwargs):
472470
"""
473471
Add an inset indicator to the axes. This is a rectangle on the plot
474472
at the position indicated by *bounds* that optionally has lines that
@@ -478,7 +476,6 @@ def indicate_inset(self, bounds, inset_ax=None, *, transform=None,
478476
--------
479477
This method is experimental as of 3.0, and the API may change.
480478
481-
482479
Parameters
483480
----------
484481
bounds : [x0, y0, width, height]
@@ -510,7 +507,9 @@ def indicate_inset(self, bounds, inset_ax=None, *, transform=None,
510507
(just below the default level of inset axes).
511508
512509
**kwargs
513-
Other keyword arguments are passed on to the rectangle patch.
510+
Other keyword arguments are passed on to the `.Rectangle` patch:
511+
512+
%(Rectangle)s
514513
515514
Returns
516515
-------
@@ -530,13 +529,13 @@ def indicate_inset(self, bounds, inset_ax=None, *, transform=None,
530529

531530
if transform is None:
532531
transform = self.transData
533-
label = kwargs.pop('label', 'indicate_inset')
532+
kwargs.setdefault('label', 'indicate_inset')
534533

535534
x, y, width, height = bounds
536535
rectangle_patch = mpatches.Rectangle(
537536
(x, y), width, height,
538537
facecolor=facecolor, edgecolor=edgecolor, alpha=alpha,
539-
zorder=zorder, label=label, transform=transform, **kwargs)
538+
zorder=zorder, transform=transform, **kwargs)
540539
self.add_patch(rectangle_patch)
541540

542541
connects = []

lib/matplotlib/axes/_base.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def set_prop_cycle(self, *args, **kwargs):
150150
# This should make a copy
151151
self._prop_keys = prop_cycler.keys
152152

153-
def __call__(self, *args, **kwargs):
153+
def __call__(self, *args, data=None, **kwargs):
154154
self.axes._process_unit_info(kwargs=kwargs)
155155

156156
for pos_only in "xy":
@@ -161,9 +161,7 @@ def __call__(self, *args, **kwargs):
161161
if not args:
162162
return
163163

164-
# Process the 'data' kwarg.
165-
data = kwargs.pop("data", None)
166-
if data is not None:
164+
if data is not None: # Process the 'data' kwarg.
167165
replaced = [mpl._replacer(data, arg) for arg in args]
168166
if len(args) == 1:
169167
label_namer_idx = 0
@@ -444,7 +442,7 @@ def __init__(self, fig, rect,
444442
self.set_label(label)
445443
self.set_figure(fig)
446444
self.set_box_aspect(box_aspect)
447-
self.set_axes_locator(kwargs.get("axes_locator", None))
445+
self._axes_locator = None # Optionally set via update(kwargs).
448446

449447
self.spines = self._gen_axes_spines()
450448

lib/matplotlib/backend_bases.py

Lines changed: 10 additions & 11 deletions
F438
Original file line numberDiff line numberDiff line change
@@ -1987,9 +1987,11 @@ def _get_output_canvas(self, backend, fmt):
19871987
"Format {!r} is not supported (supported formats: {})"
19881988
.format(fmt, ", ".join(sorted(self.get_supported_filetypes()))))
19891989

1990-
def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
1991-
orientation='portrait', format=None,
1992-
*, bbox_inches=None, backend=None, **kwargs):
1990+
def print_figure(
1991+
self, filename, dpi=None, facecolor=None, edgecolor=None,
1992+
orientation='portrait', format=None, *,
1993+
bbox_inches=None, pad_inches=None, bbox_extra_artists=None,
1994+
backend=None, **kwargs):
19931995
"""
19941996
Render the figure to hardcopy. Set the figure patch face and edge
19951997
colors. This is useful because some of the GUIs have a gray figure
@@ -2086,14 +2088,11 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
20862088
functools.partial(
20872089
print_method, dpi=dpi, orientation=orientation))
20882090
self.figure.draw(renderer)
2089-
bbox_artists = kwargs.pop("bbox_extra_artists", None)
2090-
bbox_inches = self.figure.get_tightbbox(renderer,
2091-
bbox_extra_artists=bbox_artists)
2092-
pad = kwargs.pop("pad_inches", None)
2093-
if pad is None:
2094-
pad = rcParams['savefig.pad_inches']
2095-
2096-
bbox_inches = bbox_inches.padded(pad)
2091+
bbox_inches = self.figure.get_tightbbox(
2092+
renderer, bbox_extra_artists=bbox_extra_artists)
2093+
if pad_inches is None:
2094+
pad_inches = rcParams['savefig.pad_inches']
2095+
bbox_inches = bbox_inches.padded(pad_inches)
20972096

20982097
# call adjust_bbox to save only the given area
20992098
restore_bbox = tight_bbox.adjust_bbox(self.figure, bbox_inches,

lib/matplotlib/backends/backend_cairo.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,9 +423,8 @@ def print_svg(self, fobj, *args, **kwargs):
423423
def print_svgz(self, fobj, *args, **kwargs):
424424
return self._save(fobj, 'svgz', *args, **kwargs)
425425

426-
def _save(self, fo, fmt, **kwargs):
426+
def _save(self, fo, fmt, *, orientation='portrait', **kwargs):
427427
# save PDF/PS/SVG
428-
orientation = kwargs.get('orientation', 'portrait')
429428

430429
dpi = 72
431430
self.figure.dpi = dpi

lib/matplotlib/contour.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -732,9 +732,10 @@ class ContourSet(cm.ScalarMappable, ContourLabeler):
732732

733733
def __init__(self, ax, *args,
734734
levels=None, filled=False, linewidths=None, linestyles=None,
735-
alpha=None, origin=None, extent=None,
735+
hatches=(None,), alpha=None, origin=None, extent=None,
736736
cmap=None, colors=None, norm=None, vmin=None, vmax=None,
737-
extend='neither', antialiased=None,
737+
extend='neither', antialiased=None, nchunk=0, locator=None,
738+
transform=None,
738739
**kwargs):
739740
"""
740741
Draw contour lines or filled regions, depending on
@@ -785,7 +786,7 @@ def __init__(self, ax, *args,
785786
self.filled = filled
786787
self.linewidths = linewidths
787788
self.linestyles = linestyles
788-
self.hatches = kwargs.pop('hatches', [None])
789+
self.hatches = hatches
789790
self.alpha = alpha
790791
self.origin = origin
791792
self.extent = extent
@@ -798,8 +799,8 @@ def __init__(self, ax, *args,
798799
# The default for line contours will be taken from the
799800
# LineCollection default, which uses :rc:`lines.antialiased`.
800801

801-
self.nchunk = kwargs.pop('nchunk', 0)
802-
self.locator = kwargs.pop('locator', None)
802+
self.nchunk = nchunk
803+
self.locator = locator
803804
if (isinstance(norm, mcolors.LogNorm)
804805
or isinstance(self.locator, ticker.LogLocator)):
805806
self.logscale = True
@@ -817,7 +818,7 @@ def __init__(self, ax, *args,
817818
if self.origin == 'image':
818819
self.origin = mpl.rcParams['image.origin']
819820

820-
self._transform = kwargs.pop('transform', None)
821+
self._transform = transform
821822

822823
kwargs = self._process_args(*args, **kwargs)
823824
self._process_levels()
@@ -1396,7 +1397,7 @@ class QuadContourSet(ContourSet):
13961397
levels for filled contours. See :meth:`_process_colors` method.
13971398
"""
13981399

1399-
def _process_args(self, *args, **kwargs):
1400+
def _process_args(self, *args, corner_mask=None, **kwargs):
14001401
"""
14011402
Process args and kwargs.
14021403
"""
@@ -1412,9 +1413,9 @@ def _process_args(self, *args, **kwargs):
14121413
else:
14131414
import matplotlib._contour as _contour
14141415

1415-
self._corner_mask = kwargs.pop('corner_mask', None)
1416-
if self._corner_mask is None:
1417-
self._corner_mask = mpl.rcParams['contour.corner_mask']
1416+
if corner_mask is None:
1417+
corner_mask = mpl.rcParams['contour.corner_mask']
1418+
self._corner_mask = corner_mask
14181419

14191420
x, y, z = self._contour_args(args, kwargs)
14201421

lib/matplotlib/tests/test_axes.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2060,8 +2060,7 @@ def get_next_color():
20602060
get_next_color_func=get_next_color)
20612061

20622062

2063-
def _params(c=None, xsize=2, **kwargs):
2064-
edgecolors = kwargs.pop('edgecolors', None)
2063+
def _params(c=None, xsize=2, *, edgecolors=None, **kwargs):
20652064
return (c, edgecolors, kwargs if kwargs is not None else {}, xsize)
20662065
_result = namedtuple('_result', 'c, colors')
20672066

lib/mpl_toolkits/axisartist/floating_axes.py

Lines changed: 1 addition & 11 dele A86A tions
Original file line numberDiff line numberDiff line change
@@ -320,17 +320,7 @@ def __init__(self, *args, **kwargs):
320320
self.adjust_axes_lim()
321321

322322
def _gen_axes_patch(self):
323-
"""
324-
Returns the patch used to draw the background of the axes. It
325-
is also used as the clipping path for any data elements on the
326-
axes.
327-
328-
In the standard axes, this is a rectangle, but in other
329-
projections it may not be.
330-
331-
.. note::
332-
Intended to be overridden by new projection types.
333-
"""
323+
# docstring inherited
334324
grid_helper = self.get_grid_helper()
335325
t = grid_helper.get_boundary()
336326
return mpatches.Polygon(t)

0 commit comments

Comments
 (0)
0