Skip to content

Commit fd3f25e

Browse files
authored
Merge pull request #21049 from timhoffm/simplify-legend-attrs
Simplify setting Legend attributes
2 parents 1a0ebbb + 6f6ba29 commit fd3f25e

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

lib/matplotlib/legend.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -388,17 +388,20 @@ def __init__(
388388
#: instance.
389389
self._custom_handler_map = handler_map
390390

391-
locals_view = locals()
392-
for name in ["numpoints", "markerscale", "shadow", "columnspacing",
393-
"scatterpoints", "handleheight", 'borderpad',
394-
'labelspacing', 'handlelength', 'handletextpad',
395-
'borderaxespad']:
396-
if locals_view[name] is None:
397-
value = mpl.rcParams["legend." + name]
398-
else:
399-
value = locals_view[name]
400-
setattr(self, name, value)
401-
del locals_view
391+
def val_or_rc(val, rc_name):
392+
return val if val is not None else mpl.rcParams[rc_name]
393+
394+
self.numpoints = val_or_rc(numpoints, 'legend.numpoints')
395+
self.markerscale = val_or_rc(markerscale, 'legend.markerscale')
396+
self.scatterpoints = val_or_rc(scatterpoints, 'legend.scatterpoints')
397+
self.borderpad = val_or_rc(borderpad, 'legend.borderpad')
398+
self.labelspacing = val_or_rc(labelspacing, 'legend.labelspacing')
399+
self.handlelength = val_or_rc(handlelength, 'legend.handlelength')
400+
self.handleheight = val_or_rc(handleheight, 'legend.handleheight')
401+
self.handletextpad = val_or_rc(handletextpad, 'legend.handletextpad')
402+
self.borderaxespad = val_or_rc(borderaxespad, 'legend.borderaxespad')
403+
self.columnspacing = val_or_rc(columnspacing, 'legend.columnspacing')
404+
self.shadow = val_or_rc(shadow, 'legend.shadow')
402405
# trim handles and labels if illegal label...
403406
_lab, _hand = [], []
404407
for label, handle in zip(labels, handles):

0 commit comments

Comments
 (0)