Skip to content

Commit a00b49b

Browse files
committed
FIX: colorbar with boundary norm, proportional, extend
1 parent 039c94a commit a00b49b

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

lib/matplotlib/colorbar.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ def __init__(self, ax, mappable=None, *, cmap=None,
447447
self.spacing = spacing
448448
self.orientation = orientation
449449
self.drawedges = drawedges
450+
450451
self.filled = filled
451452
self.extendfrac = extendfrac
452453
self.extendrect = extendrect
@@ -1198,8 +1199,9 @@ def _proportional_y(self):
11981199
a proportional colorbar, plus extension lengths if required:
11991200
"""
12001201
if isinstance(self.norm, colors.BoundaryNorm):
1201-
y = (self._boundaries - self._boundaries[0])
1202-
y = y / (self._boundaries[-1] - self._boundaries[0])
1202+
y = (self._boundaries - self._boundaries[self._inside][0])
1203+
y = y / (self._boundaries[self._inside][-1] -
1204+
self._boundaries[self._inside][0])
12031205
# need yscaled the same as the axes scale to get
12041206
# the extend lengths.
12051207
if self.spacing == 'uniform':

lib/matplotlib/tests/test_colorbar.py

+27
Original file line numberDiff line numberDiff line change
@@ -827,3 +827,30 @@ def test_aspects():
827827
np.testing.assert_almost_equal(
828828
cb[1][0].ax.get_position(original=False).height * 2,
829829
cb[1][2].ax.get_position(original=False).height, decimal=2)
830+
831+
832+
@image_comparison(['proportional_colorbars.png'], remove_text=True,
833+
style='mpl20')
834+
def test_proportional_colorbars():
835+
836+
x = y = np.arange(-3.0, 3.01, 0.025)
837+
X, Y = np.meshgrid(x, y)
838+
Z1 = np.exp(-X**2 - Y**2)
839+
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
840+
Z = (Z1 - Z2) * 2
841+
842+
levels = [-1.25, -0.5, -0.125, 0.125, 0.5, 1.25]
843+
844+
cmap = mcolors.ListedColormap(['r', 'g', 'white', 'g', 'r'])
845+
cmap.set_under('yellow')
846+
cmap.set_over('cyan')
847+
norm = mcolors.BoundaryNorm(levels, cmap.N)
848+
849+
extends = ['neither', 'both']
850+
spacings = ['uniform', 'proportional']
851+
fig, axs = plt.subplots(2, 2)
852+
for i in range(2):
853+
for j in range(2):
854+
CS3 = axs[i, j].contourf(X, Y, Z, levels, cmap=cmap, norm=norm,
855+
extend=extends[i])
856+
fig.colorbar(CS3, spacing=spacings[j], ax=axs[i, j])

0 commit comments

Comments
 (0)