From e97bb70e60e6a040670756a1fcc1aba8b6f7b41f Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sat, 3 Oct 2020 12:31:07 +0200 Subject: [PATCH] Simplify Colorbar.set_label, inline Colorbar._edges. `set_label` can just forward everything to either `set_xlabel` or `set_ylabel`, which already have all the logic for handling `loc`, instead of duplicating that logic. `_edges` is really just a call to `np.dstack`, and factoring it to a separate function doesn't really help legibility. --- lib/matplotlib/colorbar.py | 36 +++++------------------------------- 1 file changed, 5 insertions(+), 31 deletions(-) diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index b9555971f238..9609d9edfe27 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -766,39 +766,12 @@ def set_label(self, label, *, loc=None, **kwargs): `~.Axes.set_ylabel`. Supported keywords are *labelpad* and `.Text` properties. """ - _pos_xy = 'y' if self.orientation == 'vertical' else 'x' - _protected_kw = [_pos_xy, 'horizontalalignment', 'ha'] - if any([k in kwargs for k in _protected_kw]): - if loc is not None: - raise TypeError(f'Specifying *loc* is disallowed when any of ' - f'its corresponding low level keyword ' - f'arguments {_protected_kw} are also supplied') - loc = 'center' + if self.orientation == "vertical": + self.ax.set_ylabel(label, loc=loc, **kwargs) else: - if loc is None: - loc = mpl.rcParams['%saxis.labellocation' % _pos_xy] - if self.orientation == 'vertical': - _api.check_in_list(('bottom', 'center', 'top'), loc=loc) - else: - _api.check_in_list(('left', 'center', 'right'), loc=loc) - if loc in ['right', 'top']: - kwargs[_pos_xy] = 1. - kwargs['horizontalalignment'] = 'right' - elif loc in ['left', 'bottom']: - kwargs[_pos_xy] = 0. - kwargs['horizontalalignment'] = 'left' - if self.orientation == 'vertical': - self.ax.set_ylabel(label, **kwargs) - else: - self.ax.set_xlabel(label, **kwargs) + self.ax.set_xlabel(label, loc=loc, **kwargs) self.stale = True - def _edges(self, X, Y): - """Return the separator line segments; helper for _add_solids.""" - # Using the non-array form of these line segments is much - # simpler than making them into arrays. - return [list(zip(X[i], Y[i])) for i in range(1, len(X) - 1)] - def _add_solids(self, X, Y, C): """Draw the colors; optionally add separators.""" # Cleanup previously set artists. @@ -814,7 +787,8 @@ def _add_solids(self, X, Y, C): self._add_solids_patches(X, Y, C, mappable) else: self._add_solids_pcolormesh(X, Y, C) - self.dividers.set_segments(self._edges(X, Y) if self.drawedges else []) + self.dividers.set_segments( + np.dstack([X, Y])[1:-1] if self.drawedges else []) def _add_solids_pcolormesh(self, X, Y, C): _log.debug('Setting pcolormesh')