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

Skip to content

Make colorbar boundaries work again #21913

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

Merged
merged 1 commit into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,8 @@ def _process_values(self):
elif isinstance(self.norm, colors.NoNorm):
# NoNorm has N blocks, so N+1 boundaries, centered on integers:
b = np.arange(self.cmap.N + 1) - .5
elif self.boundaries is not None:
b = self.boundaries
else:
# otherwise make the boundaries from the size of the cmap:
N = self.cmap.N + 1
Expand All @@ -1117,7 +1119,8 @@ def _process_values(self):
self.norm.vmax = 1
self.norm.vmin, self.norm.vmax = mtransforms.nonsingular(
self.norm.vmin, self.norm.vmax, expander=0.1)
if not isinstance(self.norm, colors.BoundaryNorm):
if (not isinstance(self.norm, colors.BoundaryNorm) and
(self.boundaries is None)):
b = self.norm.inverse(b)

self._boundaries = np.asarray(b, dtype=float)
Expand All @@ -1141,7 +1144,8 @@ def _mesh(self):
norm.vmax = self.vmax
y, extendlen = self._proportional_y()
# invert:
if isinstance(norm, (colors.BoundaryNorm, colors.NoNorm)):
if (isinstance(norm, (colors.BoundaryNorm, colors.NoNorm)) or
self.boundaries is not None):
y = y * (self.vmax - self.vmin) + self.vmin # not using a norm.
else:
y = norm.inverse(y)
Expand Down Expand Up @@ -1237,7 +1241,8 @@ def _proportional_y(self):
Return colorbar data coordinates for the boundaries of
a proportional colorbar, plus extension lengths if required:
"""
if isinstance(self.norm, colors.BoundaryNorm):
if (isinstance(self.norm, colors.BoundaryNorm) or
self.boundaries is not None):
y = (self._boundaries - self._boundaries[self._inside][0])
y = y / (self._boundaries[self._inside][-1] -
self._boundaries[self._inside][0])
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions lib/matplotlib/tests/test_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -929,3 +929,12 @@ def test_nonorm():
cmap = cm.get_cmap("viridis", len(data))
mappable = cm.ScalarMappable(norm=norm, cmap=cmap)
cbar = fig.colorbar(mappable, cax=ax, orientation="horizontal")


@image_comparison(['test_boundaries.png'], remove_text=True,
style='mpl20')
def test_boundaries():
np.random.seed(seed=19680808)
fig, ax = plt.subplots(figsize=(2, 2))
pc = ax.pcolormesh(np.random.randn(10, 10), cmap='RdBu_r')
cb = fig.colorbar(pc, ax=ax, boundaries=np.linspace(-3, 3, 7))