-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add _val_or_rc-function #26168
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
Add _val_or_rc-function #26168
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -427,11 +427,7 @@ def __init__( | |
super().__init__() | ||
|
||
if prop is None: | ||
if fontsize is not None: | ||
self.prop = FontProperties(size=fontsize) | ||
else: | ||
self.prop = FontProperties( | ||
size=mpl.rcParams["legend.fontsize"]) | ||
self.prop = FontProperties(size=mpl._val_or_rc(fontsize, "legend.fontsize")) | ||
else: | ||
self.prop = FontProperties._from_any(prop) | ||
if isinstance(prop, dict) and "size" not in prop: | ||
|
@@ -447,20 +443,17 @@ def __init__( | |
#: instance. | ||
self._custom_handler_map = handler_map | ||
|
||
def val_or_rc(val, rc_name): | ||
return val if val is not None else mpl.rcParams[rc_name] | ||
Comment on lines
-450
to
-451
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you could possibly save a bunch of method gets below by setting this and reusing it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like to warn on ineffective micro optimizations. Squeezing out nanoseconds does not make any measureable difference. A function lookup cost 30ns (Python 3.11):
If we want to improve performance (and there's a lot to gain) that would need to be guided by profiling to identify the real pain points. In other cases, code structure/readability should be the primary guideline. - Not shure what is better here, but I tend to not do the additional logic indirection for saving a method to a new variable just to save method lookups. |
||
|
||
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') | ||
self.numpoints = mpl._val_or_rc(numpoints, 'legend.numpoints') | ||
self.markerscale = mpl._val_or_rc(markerscale, 'legend.markerscale') | ||
self.scatterpoints = mpl._val_or_rc(scatterpoints, 'legend.scatterpoints') | ||
self.borderpad = mpl._val_or_rc(borderpad, 'legend.borderpad') | ||
self.labelspacing = mpl._val_or_rc(labelspacing, 'legend.labelspacing') | ||
self.handlelength = mpl._val_or_rc(handlelength, 'legend.handlelength') | ||
self.handleheight = mpl._val_or_rc(handleheight, 'legend.handleheight') | ||
self.handletextpad = mpl._val_or_rc(handletextpad, 'legend.handletextpad') | ||
self.borderaxespad = mpl._val_or_rc(borderaxespad, 'legend.borderaxespad') | ||
self.columnspacing = mpl._val_or_rc(columnspacing, 'legend.columnspacing') | ||
self.shadow = mpl._val_or_rc(shadow, 'legend.shadow') | ||
# trim handles and labels if illegal label... | ||
_lab, _hand = [], [] | ||
for label, handle in zip(labels, handles): | ||
|
@@ -533,18 +526,15 @@ def val_or_rc(val, rc_name): | |
# We use FancyBboxPatch to draw a legend frame. The location | ||
# and size of the box will be updated during the drawing time. | ||
|
||
if facecolor is None: | ||
facecolor = mpl.rcParams["legend.facecolor"] | ||
facecolor = mpl._val_or_rc(facecolor, "legend.facecolor") | ||
if facecolor == 'inherit': | ||
facecolor = mpl.rcParams["axes.facecolor"] | ||
|
||
if edgecolor is None: | ||
edgecolor = mpl.rcParams["legend.edgecolor"] | ||
edgecolor = mpl._val_or_rc(edgecolor, "legend.edgecolor") | ||
if edgecolor == 'inherit': | ||
edgecolor = mpl.rcParams["axes.edgecolor"] | ||
|
||
if fancybox is None: | ||
fancybox = mpl.rcParams["legend.fancybox"] | ||
fancybox = mpl._val_or_rc(fancybox, "legend.fancybox") | ||
|
||
self.legendPatch = FancyBboxPatch( | ||
xy=(0, 0), width=1, height=1, | ||
|
@@ -559,8 +549,7 @@ def val_or_rc(val, rc_name): | |
else "square,pad=0"), | ||
mutation_scale=self._fontsize, | ||
snap=True, | ||
visible=(frameon if frameon is not None | ||
else mpl.rcParams["legend.frameon"]) | ||
visible=mpl._val_or_rc(frameon, "legend.frameon") | ||
) | ||
self._set_artist_props(self.legendPatch) | ||
|
||
|
@@ -603,11 +592,9 @@ def val_or_rc(val, rc_name): | |
'markeredgecolor': ['get_markeredgecolor', 'get_edgecolor'], | ||
'mec': ['get_markeredgecolor', 'get_edgecolor'], | ||
} | ||
labelcolor = mpl._val_or_rc(labelcolor, 'legend.labelcolor') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could nest another |
||
if labelcolor is None: | ||
if mpl.rcParams['legend.labelcolor'] is not None: | ||
labelcolor = mpl.rcParams['legend.labelcolor'] | ||
else: | ||
labelcolor = mpl.rcParams['text.color'] | ||
labelcolor = mpl.rcParams['text.color'] | ||
if isinstance(labelcolor, str) and labelcolor in color_getters: | ||
getter_names = color_getters[labelcolor] | ||
for handle, text in zip(self.legend_handles, self.texts): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest rewording this to be more human friendly since the code already explains what you had written.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using None and
mpl.rcParams[rc_name]
is good, because that's precise and comprehensible. Optional, slightly less algorithmic variant.