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

Skip to content

Commit f22c0fc

Browse files
committed
Fix issue with colorbar extend and drawedges
1 parent c6c7ec1 commit f22c0fc

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

lib/matplotlib/colorbar.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,8 +651,13 @@ def _add_solids(self, X, Y, C):
651651
if not self.drawedges:
652652
if len(self._y) >= self.n_rasterize:
653653
self.solids.set_rasterized(True)
654-
self.dividers.set_segments(
655-
np.dstack([X, Y])[1:-1] if self.drawedges else [])
654+
start_idx = 0 if self._extend_lower() else 1
655+
if self._extend_upper():
656+
self.dividers.set_segments(
657+
np.dstack([X, Y])[start_idx:] if self.drawedges else [])
658+
else:
659+
self.dividers.set_segments(
660+
np.dstack([X, Y])[start_idx:-1] if self.drawedges else [])
656661

657662
def _add_solids_patches(self, X, Y, C, mappable):
658663
hatches = mappable.hatches * len(C) # Have enough hatches.

lib/matplotlib/tests/test_colorbar.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,30 @@ def test_proportional_colorbars():
919919
fig.colorbar(CS3, spacing=spacings[j], ax=axs[i, j])
920920

921921

922+
@pytest.mark.parametrize("extend, coloroffset, res", [
923+
('both', 1, [np.array([[0., 0.], [0., 1.]]),
924+
np.array([[1., 0.], [1., 1.]]),
925+
np.array([[2., 0.], [2., 1.]])]),
926+
('min', 0, [np.array([[0., 0.], [0., 1.]]),
927+
np.array([[1., 0.], [1., 1.]])]),
928+
('max', 0, [np.array([[1., 0.], [1., 1.]]),
929+
np.array([[2., 0.], [2., 1.]])]),
930+
('neither', -1, [np.array([[1., 0.], [1., 1.]])])
931+
])
932+
def test_colorbar_extend_drawedges(extend, coloroffset, res):
933+
cmap = plt.get_cmap("viridis")
934+
bounds = np.arange(3)
935+
nb_colors = len(bounds) + coloroffset
936+
colors = cmap(np.linspace(100, 255, nb_colors).astype(int))
937+
cmap, norm = mcolors.from_levels_and_colors(bounds, colors, extend=extend)
938+
939+
plt.figure(figsize=(5, 1))
940+
ax = plt.subplot(111)
941+
cbar = Colorbar(ax, cmap=cmap, norm=norm, orientation='horizontal',
942+
drawedges=True)
943+
assert np.all(np.equal(cbar.dividers.get_segments(), res))
944+
945+
922946
def test_negative_boundarynorm():
923947
fig, ax = plt.subplots(figsize=(1, 3))
924948
cmap = plt.get_cmap("viridis")

0 commit comments

Comments
 (0)