Skip to content

Commit 6716429

Browse files
committed
Inline some helpers in ColorbarBase.
There's so many helper functions in ColorbarBase that I find the code logic a bit hard to follow... In draw_all: - Simplify and inline _find_range as a one-liner. - _outline can also be inlined after a bit of shortening using fancy indexing, which I find easier to read anyways: we just take the first two and last two values on the first column, and then the last two and first two values on the second column. - Also everything is in data units now; the comment about self._mesh() has been outdated for a while. In _mesh: - Simplify and inline _central_N.
1 parent 48b6327 commit 6716429

File tree

1 file changed

+15
-44
lines changed

1 file changed

+15
-44
lines changed

lib/matplotlib/colorbar.py

Lines changed: 15 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -530,28 +530,29 @@ def draw_all(self):
530530
Calculate any free parameters based on the current cmap and norm,
531531
and do all the drawing.
532532
"""
533-
# sets self._boundaries and self._values in real data units.
534-
# takes into account extend values:
533+
self._config_axis() # Inline it after deprecation elapses.
534+
# Set self._boundaries and self._values, including extensions.
535535
self._process_values()
536-
# sets self.vmin and vmax in data units, but just for the part of the
537-
# colorbar that is not part of the extend patch:
538-
self._find_range()
539-
# returns the X and Y mesh, *but* this was/is in normalized units:
536+
# Set self.vmin and self.vmax to first and last boundary, excluding
537+
# extensions.
538+
self.vmin, self.vmax = self._boundaries[self._inside][[0, -1]]
539+
# Compute the X/Y mesh, assuming vertical orientation.
540540
X, Y = self._mesh()
541-
C = self._values[:, np.newaxis]
542-
543-
self._config_axis() # Inline it after deprecation elapses.
541+
# Extract bounding polygon (the last entry's value (X[0, 1]) doesn't
542+
# matter, it just matches the CLOSEPOLY code).
543+
x = np.concatenate([X[[0, 1, -2, -1], 0], X[[-1, -2, 1, 0, 0], 1]])
544+
y = np.concatenate([Y[[0, 1, -2, -1], 0], Y[[-1, -2, 1, 0, 0], 1]])
545+
xy = (np.column_stack([x, y]) if self.orientation == 'vertical' else
546+
np.column_stack([y, x])) # Apply orientation.
544547
# Configure axes limits, patch, and outline.
545-
xy = self._outline(X, Y)
546548
xmin, ymin = xy.min(axis=0)
547549
xmax, ymax = xy.max(axis=0)
548550
self.ax.set(xlim=(xmin, xmax), ylim=(ymin, ymax))
549551
self.outline.set_xy(xy)
550552
self.patch.set_xy(xy)
551553
self.update_ticks()
552-
553554
if self.filled:
554-
self._add_solids(X, Y, C)
555+
self._add_solids(X, Y, self._values[:, np.newaxis])
555556

556557
@cbook.deprecated("3.3")
557558
def config_axis(self):
@@ -800,19 +801,6 @@ def set_label(self, label, *, loc=None, **kwargs):
800801
self.ax.set_xlabel(label, **kwargs)
801802
self.stale = True
802803

803-
def _outline(self, X, Y):
804-
"""
805-
Return *x*, *y* arrays of colorbar bounding polygon,
806-
taking orientation into account.
807-
"""
808-
N = X.shape[0]
809-
ii = [0, 1, N - 2, N - 1, 2 * N - 1, 2 * N - 2, N + 1, N, 0]
810-
x = X.T.reshape(-1)[ii]
811-
y = Y.T.reshape(-1)[ii]
812-
return (np.column_stack([y, x])
813-
if self.orientation == 'horizontal' else
814-
np.column_stack([x, y]))
815-
816804
def _edges(self, X, Y):
817805
"""Return the separator line segments; helper for _add_solids."""
818806
N = X.shape[0]
@@ -1008,24 +996,6 @@ def _process_values(self, b=None):
1008996
b[-1] = b[-1] + 1
1009997
self._process_values(b)
1010998

1011-
def _find_range(self):
1012-
"""
1013-
Set :attr:`vmin` and :attr:`vmax` attributes to the first and
1014-
last boundary excluding extended end boundaries.
1015-
"""
1016-
b = self._boundaries[self._inside]
1017-
self.vmin = b[0]
1018-
self.vmax = b[-1]
1019-
1020-
def _central_N(self):
1021-
"""Return the number of boundaries excluding end extensions."""
1022-
nb = len(self._boundaries)
1023-
if self.extend == 'both':
1024-
nb -= 2
1025-
elif self.extend in ('min', 'max'):
1026-
nb -= 1
1027-
return nb
1028-
1029999
def _get_extension_lengths(self, frac, automin, automax, default=0.05):
10301000
"""
10311001
Return the lengths of colorbar extensions.
@@ -1132,7 +1102,8 @@ def _mesh(self):
11321102
norm.vmax = self.vmax
11331103
x = np.array([0.0, 1.0])
11341104
if self.spacing == 'uniform':
1135-
y = self._uniform_y(self._central_N())
1105+
n_boundaries_no_extensions = len(self._boundaries[self._inside])
1106+
y = self._uniform_y(n_boundaries_no_extensions)
11361107
else:
11371108
y = self._proportional_y()
11381109
xmid = np.array([0.5])

0 commit comments

Comments
 (0)