Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5cd6d69

Browse files
authored
Merge pull request matplotlib#21913 from jklymak/fix-colorbar-allow-boundaries
Make colorbar boundaries work again
2 parents 7129ed8 + 31efd72 commit 5cd6d69

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

lib/matplotlib/colorbar.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,8 @@ def _process_values(self):
11011101
elif isinstance(self.norm, colors.NoNorm):
11021102
# NoNorm has N blocks, so N+1 boundaries, centered on integers:
11031103
b = np.arange(self.cmap.N + 1) - .5
1104+
elif self.boundaries is not None:
1105+
b = self.boundaries
11041106
else:
11051107
# otherwise make the boundaries from the size of the cmap:
11061108
N = self.cmap.N + 1
@@ -1117,7 +1119,8 @@ def _process_values(self):
11171119
self.norm.vmax = 1
11181120
self.norm.vmin, self.norm.vmax = mtransforms.nonsingular(
11191121
self.norm.vmin, self.norm.vmax, expander=0.1)
1120-
if not isinstance(self.norm, colors.BoundaryNorm):
1122+
if (not isinstance(self.norm, colors.BoundaryNorm) and
1123+
(self.boundaries is None)):
11211124
b = self.norm.inverse(b)
11221125

11231126
self._boundaries = np.asarray(b, dtype=float)
@@ -1141,7 +1144,8 @@ def _mesh(self):
11411144
norm.vmax = self.vmax
11421145
y, extendlen = self._proportional_y()
11431146
# invert:
1144-
if isinstance(norm, (colors.BoundaryNorm, colors.NoNorm)):
1147+
if (isinstance(norm, (colors.BoundaryNorm, colors.NoNorm)) or
1148+
self.boundaries is not None):
11451149
y = y * (self.vmax - self.vmin) + self.vmin # not using a norm.
11461150
else:
11471151
y = norm.inverse(y)
@@ -1237,7 +1241,8 @@ def _proportional_y(self):
12371241
Return colorbar data coordinates for the boundaries of
12381242
a proportional colorbar, plus extension lengths if required:
12391243
"""
1240-
if isinstance(self.norm, colors.BoundaryNorm):
1244+
if (isinstance(self.norm, colors.BoundaryNorm) or
1245+
self.boundaries is not None):
12411246
y = (self._boundaries - self._boundaries[self._inside][0])
12421247
y = y / (self._boundaries[self._inside][-1] -
12431248
self._boundaries[self._inside][0])

lib/matplotlib/tests/test_colorbar.py

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

0 commit comments

Comments
 (0)