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

Skip to content

Commit d1c5a6a

Browse files
committed
FIX: fix colorbars with no scales
1 parent c56e3c5 commit d1c5a6a

File tree

5 files changed

+49
-16
lines changed

5 files changed

+49
-16
lines changed

lib/matplotlib/colorbar.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,24 +1006,34 @@ def _reset_locator_formatter_scale(self):
10061006
self.locator = None
10071007
self.minorlocator = None
10081008
self.formatter = None
1009-
if ((self.spacing == 'uniform') and
1010-
((self.boundaries is not None) or
1011-
isinstance(self.norm, colors.BoundaryNorm))):
1012-
funcs = (self._forward_boundaries, self._inverse_boundaries)
1013-
self.ax.set_xscale('function', functions=funcs)
1014-
self.ax.set_yscale('function', functions=funcs)
1015-
self.__scale = 'function'
1016-
elif hasattr(self.norm, '_scale') and (self.norm._scale is not None):
1009+
if (self.boundaries is not None or
1010+
isinstance(self.norm, colors.BoundaryNorm)):
1011+
if self.spacing == 'uniform':
1012+
funcs = (self._forward_boundaries, self._inverse_boundaries)
1013+
self.ax.set_xscale('function', functions=funcs)
1014+
self.ax.set_yscale('function', functions=funcs)
1015+
self.__scale = 'function'
1016+
elif self.spacing == 'proportional':
1017+
self.__scale = 'linear'
1018+
self.ax.set_xscale('linear')
1019+
self.ax.set_yscale('linear')
1020+
elif hasattr(self.norm, '_scale') and self.norm._scale is not None:
1021+
# use the norm's scale:
10171022
self.ax.set_xscale(self.norm._scale)
10181023
self.ax.set_yscale(self.norm._scale)
10191024
self.__scale = self.norm._scale.name
1020-
else:
1025+
elif type(self.norm) is colors.Normalize:
1026+
# plain Normalize:
10211027
self.ax.set_xscale('linear')
10221028
self.ax.set_yscale('linear')
1023-
if type(self.norm) is colors.Normalize:
1024-
self.__scale = 'linear'
1025-
else:
1026-
self.__scale = 'manual'
1029+
self.__scale = 'linear'
1030+
else:
1031+
# norm._scale is None or not an attr: derive the scale from
1032+
# the Norm:
1033+
funcs = (self.norm, self.norm.inverse)
1034+
self.ax.set_xscale('function', functions=funcs)
1035+
self.ax.set_yscale('function', functions=funcs)
1036+
self.__scale = 'function'
10271037

10281038
def _locate(self, x):
10291039
"""

lib/matplotlib/colors.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1152,7 +1152,7 @@ def __init__(self, vmin=None, vmax=None, clip=False):
11521152
self.vmin = _sanitize_extrema(vmin)
11531153
self.vmax = _sanitize_extrema(vmax)
11541154
self.clip = clip
1155-
self._scale = scale.LinearScale(axis=None)
1155+
self._scale = None # will default to LinearScale for colorbar
11561156

11571157
@staticmethod
11581158
def process_value(value):
@@ -1334,6 +1334,16 @@ def __call__(self, value, clip=None):
13341334
result = np.atleast_1d(result)[0]
13351335
return result
13361336

1337+
def inverse(self, value):
1338+
if not self.scaled():
1339+
raise ValueError("Not invertible until both vmin and vmax are set")
1340+
(vmin,), _ = self.process_value(self.vmin)
1341+
(vmax,), _ = self.process_value(self.vmax)
1342+
(vcenter,), _ = self.process_value(self.vcenter)
1343+
1344+
result = np.interp(value, [0, 0.5, 1.], [vmin, vcenter, vmax])
1345+
return result
1346+
13371347

13381348
class CenteredNorm(Normalize):
13391349
def __init__(self, vcenter=0, halfrange=None, clip=False):

lib/matplotlib/tests/test_colorbar.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,3 +771,17 @@ def test_inset_colorbar_layout():
771771
np.testing.assert_allclose(cb.ax.get_position().bounds,
772772
[0.87, 0.342, 0.0237, 0.315], atol=0.01)
773773
assert cb.ax.outer_ax in ax.child_axes
774+
775+
776+
@image_comparison(['colorbar_twoslope.png'], remove_text=True,
777+
style='mpl20')
778+
def test_twoslope_colorbar():
779+
# Note that the first tick = 20, and should be in the middle
780+
# of the colorbar (white)
781+
fig, ax = plt.subplots()
782+
783+
norm = mcolors.TwoSlopeNorm(20, 0, 100)
784+
pc = ax.pcolormesh(np.arange(1, 11), np.arange(1, 11),
785+
np.arange(100).reshape(10, 10),
786+
norm=norm, cmap='RdBu_r')
787+
fig.colorbar(pc)

lib/matplotlib/tests/test_colors.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,5 @@ def test_norm_deepcopy():
14061406
norm = mcolors.Normalize()
14071407
norm.vmin = 0.0002
14081408
norm2 = copy.deepcopy(norm)
1409-
assert isinstance(norm2._scale, mscale.LinearScale)
1409+
assert norm2._scale is None
14101410
assert norm2.vmin == norm.vmin
1411-
assert norm2._scale is not norm._scale

0 commit comments

Comments
 (0)