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

Skip to content

FIX: fix colorbars with no scales #20327

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
Jun 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
36 changes: 23 additions & 13 deletions lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -1006,24 +1006,34 @@ def _reset_locator_formatter_scale(self):
self.locator = None
self.minorlocator = None
self.formatter = None
if ((self.spacing == 'uniform') and
((self.boundaries is not None) or
isinstance(self.norm, colors.BoundaryNorm))):
funcs = (self._forward_boundaries, self._inverse_boundaries)
self.ax.set_xscale('function', functions=funcs)
self.ax.set_yscale('function', functions=funcs)
self.__scale = 'function'
elif hasattr(self.norm, '_scale') and (self.norm._scale is not None):
if (self.boundaries is not None or
isinstance(self.norm, colors.BoundaryNorm)):
if self.spacing == 'uniform':
funcs = (self._forward_boundaries, self._inverse_boundaries)
self.ax.set_xscale('function', functions=funcs)
self.ax.set_yscale('function', functions=funcs)
self.__scale = 'function'
elif self.spacing == 'proportional':
self.__scale = 'linear'
self.ax.set_xscale('linear')
self.ax.set_yscale('linear')
elif hasattr(self.norm, '_scale') and self.norm._scale is not None:
# use the norm's scale:
self.ax.set_xscale(self.norm._scale)
self.ax.set_yscale(self.norm._scale)
self.__scale = self.norm._scale.name
else:
elif type(self.norm) is colors.Normalize:
# plain Normalize:
self.ax.set_xscale('linear')
self.ax.set_yscale('linear')
if type(self.norm) is colors.Normalize:
self.__scale = 'linear'
else:
self.__scale = 'manual'
self.__scale = 'linear'
else:
# norm._scale is None or not an attr: derive the scale from
# the Norm:
funcs = (self.norm, self.norm.inverse)
self.ax.set_xscale('function', functions=funcs)
self.ax.set_yscale('function', functions=funcs)
self.__scale = 'function'

def _locate(self, x):
"""
Expand Down
12 changes: 11 additions & 1 deletion lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1152,7 +1152,7 @@ def __init__(self, vmin=None, vmax=None, clip=False):
self.vmin = _sanitize_extrema(vmin)
self.vmax = _sanitize_extrema(vmax)
self.clip = clip
self._scale = scale.LinearScale(axis=None)
self._scale = None # will default to LinearScale for colorbar
Copy link
Member Author

Choose a reason for hiding this comment

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

Note downstream libraries will likely derive from Normalize, so scale should be None so their norm gets used...

Copy link
Member Author

Choose a reason for hiding this comment

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

@dstansby can you see if this version works?

Copy link
Member

Choose a reason for hiding this comment

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

Yep, seems to work well with this version.

Copy link
Member Author

Choose a reason for hiding this comment

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

Any chance of a formal review?


@staticmethod
def process_value(value):
Expand Down Expand Up @@ -1334,6 +1334,16 @@ def __call__(self, value, clip=None):
result = np.atleast_1d(result)[0]
return result

def inverse(self, value):
if not self.scaled():
raise ValueError("Not invertible until both vmin and vmax are set")
(vmin,), _ = self.process_value(self.vmin)
(vmax,), _ = self.process_value(self.vmax)
(vcenter,), _ = self.process_value(self.vcenter)

result = np.interp(value, [0, 0.5, 1.], [vmin, vcenter, vmax])
return result


class CenteredNorm(Normalize):
def __init__(self, vcenter=0, halfrange=None, clip=False):
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions lib/matplotlib/tests/test_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,3 +771,17 @@ def test_inset_colorbar_layout():
np.testing.assert_allclose(cb.ax.get_position().bounds,
[0.87, 0.342, 0.0237, 0.315], atol=0.01)
assert cb.ax.outer_ax in ax.child_axes


@image_comparison(['colorbar_twoslope.png'], remove_text=True,
style='mpl20')
def test_twoslope_colorbar():
# Note that the first tick = 20, and should be in the middle
Copy link
Contributor

Choose a reason for hiding this comment

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

Adding the ticklabels to the image would be helpful here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I agree, but we really try to avoid labels, because they cause image comparison problems when the font library changes...

# of the colorbar (white)
fig, ax = plt.subplots()

norm = mcolors.TwoSlopeNorm(20, 0, 100)
pc = ax.pcolormesh(np.arange(1, 11), np.arange(1, 11),
np.arange(100).reshape(10, 10),
norm=norm, cmap='RdBu_r')
fig.colorbar(pc)
3 changes: 1 addition & 2 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,5 @@ def test_norm_deepcopy():
norm = mcolors.Normalize()
norm.vmin = 0.0002
norm2 = copy.deepcopy(norm)
assert isinstance(norm2._scale, mscale.LinearScale)
assert norm2._scale is None
assert norm2.vmin == norm.vmin
assert norm2._scale is not norm._scale