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

Skip to content

Displaying colorbars with specified boundaries correctly #17453

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

Closed
wants to merge 1 commit into from
Closed
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
36 changes: 33 additions & 3 deletions lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,8 +575,8 @@ def _get_ticker_locator_formatter(self):
else:
locator = _ColorbarAutoLocator(self)
else:
b = self._boundaries[self._inside]
locator = ticker.FixedLocator(b, nbins=10)
b = self.boundaries[self._inside]
locator = ticker.FixedLocator(b)

if formatter is None:
if isinstance(self.norm, colors.LogNorm):
Expand Down Expand Up @@ -892,7 +892,37 @@ def _process_values(self, b=None):
b = self.boundaries
if b is not None:
self._boundaries = np.asarray(b, dtype=float)
if self.values is None:
if self.values is None and self.boundaries is not None:
# define the outside additions
if self.extend == 'min':
to_add = self._boundaries[0]
elif self.extend == 'max':
to_add = self._boundaries[-1]
elif self.extend == 'both':
to_add = self._boundaries[[0, -1]]
elif self.extend == 'neither':
to_add = []
# add values to the colorbar insides
boundaries_inside = self._boundaries[self._inside]
n_between = max((self.cmap.N-len(to_add)) //
(len(boundaries_inside)-1), 1)
self._values = np.linspace(boundaries_inside[:-1],
boundaries_inside[1:],
n_between, endpoint=False,
axis=-1).flatten()
self._values = np.append(self._values, boundaries_inside[-1])
# add back in the boundaries for the extensions
self._boundaries = np.sort(np.concatenate([to_add,
self._values]))
# calculate values at which to plot colors
self._values = 0.5 * (self._values[:-1] + self._values[1:])
# add back in the boundaries for the extensions, display
# colors of extensions at boundary values
self._values = np.sort(np.concatenate([to_add,
self._values]))
if isinstance(self.norm, colors.NoNorm):
self._values = (self._values + 0.00001).astype(np.int16)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would np.round be a better option here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed only the indentation on line 924. I'll have to investigate what it does.

elif self.values is None and self.boundaries is None:
self._values = 0.5 * (self._boundaries[:-1]
+ self._boundaries[1:])
if isinstance(self.norm, colors.NoNorm):
Expand Down