8000 Merge pull request #21926 from QuLogic/auto-backport-of-pr-21913-on-v… · matplotlib/matplotlib@2d84c62 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2d84c62

Browse files
authored
Merge pull request #21926 from QuLogic/auto-backport-of-pr-21913-on-v3.5.x
Backport PR #21913 on branch v3.5.x (Make colorbar boundaries work again)
2 parents 3b38f2f + 6d14827 commit 2d84c62

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

lib/matplotlib/colorbar.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,8 @@ def _process_values(self):
10941094
elif isinstance(self.norm, colors.NoNorm):
10951095
# NoNorm has N blocks, so N+1 boundaries, centered on integers:
10961096
b = np.arange(self.cmap.N + 1) - .5
1097+
elif self.boundaries is not None:
1098+
b = self.boundaries
10971099
else:
10981100
# otherwise make the boundaries from the size of the cmap:
10991101
N = self.cmap.N + 1
@@ -1110,7 +1112,8 @@ def _process_values(self):
11101112
self.norm.vmax = 1
11111113
self.norm.vmin, self.norm.vmax = mtransforms.nonsingular(
11121114
self.norm.vmin, self.norm.vmax, expander=0.1)
1113-
if not isinstance(self.norm, colors.BoundaryNorm):
1115+
if (not isinstance(self.norm, colors.BoundaryNorm) and
1116+
(self.boundaries is None)):
11141117
b = self.norm.inverse(b)
11151118

11161119
self._boundaries = np.asarray(b, dtype=float)
@@ -1132,18 +1135,15 @@ def _mesh(self):
11321135
norm = copy.deepcopy(self.norm)
11331136
norm.vmin = self.vmin
11341137
norm.vmax = self.vmax
1135-
x = np.array([0.0, 1.0])
11361138
y, extendlen = self._proportional_y()
11371139
# invert:
11381140
if (isinstance(norm, (colors.BoundaryNorm, colors.NoNorm)) or
1139-
(self.__scale == 'manual')):
1140-
# if a norm doesn't have a named scale, or we are not using a norm:
1141-
dv = self.vmax - self.vmin
1142-
y = y * dv + self.vmin
1141+
self.boundaries is not None):
1142+
y = y * (self.vmax - self.vmin) + self.vmin # not using a norm.
11431143
else:
11441144
y = norm.inverse(y)
11451145
self._y = y
1146-
X, Y = np.meshgrid(x, y)
1146+
X, Y = np.meshgrid([0., 1.], y)
11471147
if self.orientation == 'vertical':
11481148
return (X, Y, extendlen)
11491149
else:
@@ -1183,8 +1183,8 @@ def _reset_locator_formatter_scale(self):
11831183
self._set_scale('function', functions=funcs)
11841184
elif self.spacing == 'proportional':
11851185
self._set_scale('linear')
1186-
elif hasattr(self.norm, '_scale') and self.norm._scale is not None:
1187-
# use the norm's scale:
1186+
elif getattr(self.norm, '_scale', None):
1187+
# use the norm's scale (if it exists and is not None):
11881188
self._set_scale(self.norm._scale)
11891189
elif type(self.norm) is colors.Normalize:
11901190
# plain Normalize:
@@ -1234,7 +1234,8 @@ def _proportional_y(self):
12341234
Return colorbar data coordinates for the boundaries of
12351235
a proportional colorbar, plus extension lengths if required:
12361236
"""
1237-
if isinstance(self.norm, colors.BoundaryNorm):
1237+
if (isinstance(self.norm, colors.BoundaryNorm) or
1238+
self.boundaries is not None):
12381239
y = (self._boundaries - self._boundaries[self._inside][0])
12391240
y = y / (self._boundaries[self._inside][-1] -
12401241
self._boundaries[self._inside][0])
Loading

lib/matplotlib/tests/test_colorbar.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,3 +927,12 @@ def test_nonorm():
927927
cmap = cm.get_cmap("viridis", len(data))
928928
mappable = cm.ScalarMappable(norm=norm, cmap=cmap)
929929
cbar = fig.colorbar(mappable, cax=ax, orientation="horizontal")
930+
931+
932+
@image_comparison(['test_boundaries.png'], remove_text=True,
933+
style='mpl20')
934+
def test_boundaries():
935+
np.random.seed(seed=19680808)
936+
fig, ax = plt.subplots(figsize=(2, 2))
937+
pc = ax.pcolormesh(np.random.randn(10, 10), cmap='RdBu_r')
938+
cb = fig.colorbar(pc, ax=ax, boundaries=np.linspace(-3, 3, 7))

0 commit comments

Comments
 (0)
0