Skip to content

Simplify setting Legend attributes #21049

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 13, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions lib/matplotlib/legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,17 +385,20 @@ def __init__(
#: instance.
self._custom_handler_map = handler_map

locals_view = locals()
for name in ["numpoints", "markerscale", "shadow", "columnspacing",
"scatterpoints", "handleheight", 'borderpad',
'labelspacing', 'handlelength', 'handletextpad',
'borderaxespad']:
if locals_view[name] is None:
value = mpl.rcParams["legend." + name]
else:
value = locals_view[name]
setattr(self, name, value)
del locals_view
def val_or_rc(val, rc_name):
return val if val is not None else mpl.rcParams[rc_name]

self.numpoints = val_or_rc(numpoints, 'legend.numpoints')
self.markerscale = val_or_rc(markerscale, 'legend.markerscale')
self.scatterpoints = val_or_rc(scatterpoints, 'legend.scatterpoints')
self.borderpad = val_or_rc(borderpad, 'legend.borderpad')
self.labelspacing = val_or_rc(labelspacing, 'legend.labelspacing')
self.handlelength = val_or_rc(handlelength, 'legend.handlelength')
self.handleheight = val_or_rc(handleheight, 'legend.handleheight')
self.handletextpad = val_or_rc(handletextpad, 'legend.handletextpad')
self.borderaxespad = val_or_rc(borderaxespad, 'legend.borderaxespad')
self.columnspacing = val_or_rc(columnspacing, 'legend.columnspacing')
self.shadow = val_or_rc(shadow, 'legend.shadow')
# trim handles and labels if illegal label...
_lab, _hand = [], []
for label, handle in zip(labels, handles):
Expand Down