Skip to content

Commit c81cc57

Browse files
authored
Merge pull request #23282 from meeseeksmachine/auto-backport-of-pr-22865-on-v3.5.x
Backport PR #22865 on branch v3.5.x (Fix issue with colorbar extend and drawedges)
2 parents 02219ee + de4c559 commit c81cc57

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

lib/matplotlib/colorbar.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -627,8 +627,12 @@ def _add_solids(self, X, Y, C):
627627
if not self.drawedges:
628628
if len(self._y) >= self.n_rasterize:
629629
self.solids.set_rasterized(True)
630-
self.dividers.set_segments(
631-
np.dstack([X, Y])[1:-1] if self.drawedges else [])
630+
if self.drawedges:
631+
start_idx = 0 if self._extend_lower() else 1
632+
end_idx = len(X) if self._extend_upper() else -1
633+
self.dividers.set_segments(np.dstack([X, Y])[start_idx:end_idx])
634+
else:
635+
self.dividers.set_segments([])
632636

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

lib/matplotlib/tests/test_colorbar.py

+24
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,30 @@ def test_proportional_colorbars():
929929
fig.colorbar(CS3, spacing=spacings[j], ax=axs[i, j])
930930

931931

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

0 commit comments

Comments
 (0)