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

Skip to content

Commit 00cdf28

Browse files
authored
Merge pull request #22865 from matplotlib/drawedgesextend
Fix issue with colorbar extend and drawedges
2 parents a1747a5 + c6729ba commit 00cdf28

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

lib/matplotlib/colorbar.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,8 +651,12 @@ 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+
if self.drawedges:
655+
start_idx = 0 if self._extend_lower() else 1
656+
end_idx = len(X) if self._extend_upper() else -1
657+
self.dividers.set_segments(np.dstack([X, Y])[start_idx:end_idx])
658+
else:
659+
self.dividers.set_segments([])
656660

657661
def _add_solids_patches(self, X, Y, C, mappable):
658662
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
@@ -931,6 +931,30 @@ def test_proportional_colorbars():
931931
fig.colorbar(CS3, spacing=spacings[j], ax=axs[i, j])
932932

933933

934+
@pytest.mark.parametrize("extend, coloroffset, res", [
935+
('both', 1, [np.array([[0., 0.], [0., 1.]]),
936+
np.array([[1., 0.], [1., 1.]]),
937+
np.array([[2., 0.], [2., 1.]])]),
938+
('min', 0, [np.array([[0., 0.], [0., 1.]]),
939+
np.array([[1., 0.], [1., 1.]])]),
940+
('max', 0, [np.array([[1., 0.], [1., 1.]]),
941+
np.array([[2., 0.], [2., 1.]])]),
942+
('neither', -1, [np.array([[1., 0.], [1., 1.]])])
943+
])
944+
def test_colorbar_extend_drawedges(extend, coloroffset, res):
945+
cmap = plt.get_cmap("viridis")
946+
bounds = np.arange(3)
947+
nb_colors = len(bounds) + coloroffset
948+
colors = cmap(np.linspace(100, 255, nb_colors).astype(int))
949+
cmap, norm = mcolors.from_levels_and_colors(bounds, colors, extend=extend)
950+
951+
plt.figure(figsize=(5, 1))
952+
ax = plt.subplot(111)
953+
cbar = Colorbar(ax, cmap=cmap, norm=norm, orientation='horizontal',
954+
drawedges=True)
955+
assert np.all(np.equal(cbar.dividers.get_segments(), res))
956+
957+
934958
def test_negative_boundarynorm():
935959
fig, ax = plt.subplots(figsize=(1, 3))
936960
cmap = plt.get_cmap("viridis")

0 commit comments

Comments
 (0)