Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2177d8c

Browse filesBrowse files
committed
FIX: fix colorbars with no scales
1 parent c56e3c5 commit 2177d8c
Copy full SHA for 2177d8c

File tree

Expand file treeCollapse file tree

4 files changed

+49
-12
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+49
-12
lines changed

‎lib/matplotlib/colorbar.py

Copy file name to clipboardExpand all lines: lib/matplotlib/colorbar.py
+25-12Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,13 @@ def _inverse_boundaries(self, x):
996996
b = self._boundaries
997997
return np.interp(x, np.linspace(0, b[-1], len(b)), b)
998998

999+
def _forward_from_norm(self, x):
1000+
y = self.norm(x)
1001+
return y
1002+
1003+
def _inverse_from_norm(self, x):
1004+
return self.norm.inverse(x)
1005+
9991006
def _reset_locator_formatter_scale(self):
10001007
"""
10011008
Reset the locator et al to defaults. Any user-hardcoded changes
@@ -1006,24 +1013,30 @@ def _reset_locator_formatter_scale(self):
10061013
self.locator = None
10071014
self.minorlocator = None
10081015
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+
if (self.boundaries is not None or
1017+
isinstance(self.norm, colors.BoundaryNorm)):
1018+
if self.spacing == 'uniform':
1019+
funcs = (self._forward_boundaries, self._inverse_boundaries)
1020+
self.ax.set_xscale('function', functions=funcs)
1021+
self.ax.set_yscale('function', functions=funcs)
1022+
self.__scale = 'function'
1023+
elif self.spacing == 'proportional':
1024+
self.__scale = 'linear'
1025+
self.ax.set_xscale('linear')
1026+
self.ax.set_yscale('linear')
10161027
elif hasattr(self.norm, '_scale') and (self.norm._scale is not None):
10171028
self.ax.set_xscale(self.norm._scale)
10181029
self.ax.set_yscale(self.norm._scale)
10191030
self.__scale = self.norm._scale.name
1020-
else:
1031+
elif type(self.norm) is colors.Normalize:
1032+
self.__scale = 'linear'
10211033
self.ax.set_xscale('linear')
10221034
self.ax.set_yscale('linear')
1023-
if type(self.norm) is colors.Normalize:
1024-
self.__scale = 'linear'
1025-
else:
1026-
self.__scale = 'manual'
1035+
else:
1036+
funcs = (self._forward_from_norm, self._inverse_from_norm)
1037+
self.ax.set_xscale('function', functions=funcs)
1038+
self.ax.set_yscale('function', functions=funcs)
1039+
self.__scale = 'function'
10271040

10281041
def _locate(self, x):
10291042
"""

‎lib/matplotlib/colors.py

Copy file name to clipboardExpand all lines: lib/matplotlib/colors.py
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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):
Loading

‎lib/matplotlib/tests/test_colorbar.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_colorbar.py
+14Lines 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)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.