Skip to content

Commit fd4bd80

Browse files
QuLogicmeeseeksmachine
authored andcommitted
Backport PR #23684: Fix rectangle and hatches for colorbar
1 parent 3748ad5 commit fd4bd80

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

lib/matplotlib/colorbar.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -606,10 +606,13 @@ def _update_dividers(self):
606606
self.dividers.set_segments(segments)
607607

608608
def _add_solids_patches(self, X, Y, C, mappable):
609-
hatches = mappable.hatches * len(C) # Have enough hatches.
609+
hatches = mappable.hatches * (len(C) + 1) # Have enough hatches.
610+
if self._extend_lower():
611+
# remove first hatch that goes into the extend patch
612+
hatches = hatches[1:]
610613
patches = []
611614
for i in range(len(X) - 1):
612-
xy = np.array([[X[i, 0], Y[i, 0]],
615+
xy = np.array([[X[i, 0], Y[i, 1]],
613616
[X[i, 1], Y[i, 0]],
614617
[X[i + 1, 1], Y[i + 1, 0]],
615618
[X[i + 1, 0], Y[i + 1, 1]]])
@@ -661,9 +664,9 @@ def _do_extends(self, ax=None):
661664
mappable = getattr(self, 'mappable', None)
662665
if (isinstance(mappable, contour.ContourSet)
663666
and any(hatch is not None for hatch in mappable.hatches)):
664-
hatches = mappable.hatches
667+
hatches = mappable.hatches * (len(self._y) + 1)
665668
else:
666-
hatches = [None]
669+
hatches = [None] * (len(self._y) + 1)
667670

668671
if self._extend_lower():
669672
if not self.extendrect:
@@ -687,6 +690,8 @@ def _do_extends(self, ax=None):
687690
zorder=np.nextafter(self.ax.patch.zorder, -np.inf))
688691
self.ax.add_patch(patch)
689692
self._extend_patches.append(patch)
693+
# remove first hatch that goes into the extend patch
694+
hatches = hatches[1:]
690695
if self._extend_upper():
691696
if not self.extendrect:
692697
# triangle
@@ -699,10 +704,12 @@ def _do_extends(self, ax=None):
699704
# add the patch
700705
val = 0 if self._long_axis().get_inverted() else -1
701706
color = self.cmap(self.norm(self._values[val]))
707+
hatch_idx = len(self._y) - 1
702708
patch = mpatches.PathPatch(
703709
mpath.Path(xy), facecolor=color, alpha=self.alpha,
704710
linewidth=0, antialiased=False,
705-
transform=self.ax.transAxes, hatch=hatches[-1], clip_on=False,
711+
transform=self.ax.transAxes, hatch=hatches[hatch_idx],
712+
clip_on=False,
706713
# Place it right behind the standard patches, which is
707714
# needed if we updated the extends
708715
zorder=np.nextafter(self.ax.patch.zorder, -np.inf))

lib/matplotlib/tests/test_colorbar.py

+30
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,36 @@ def test_colorbar_extend_drawedges():
998998
np.testing.assert_array_equal(cbar.dividers.get_segments(), res)
999999

10001000

1001+
@image_comparison(['contourf_extend_patches.png'], remove_text=True,
1002+
style='mpl20')
1003+
def test_colorbar_contourf_extend_patches():
1004+
params = [
1005+
('both', 5, ['\\', '//']),
1006+
('min', 7, ['+']),
1007+
('max', 2, ['|', '-', '/', '\\', '//']),
1008+
('neither', 10, ['//', '\\', '||']),
1009+
]
1010+
1011+
plt.rcParams['axes.linewidth'] = 2
1012+
1013+
fig = plt.figure(figsize=(10, 4))
1014+
subfigs = fig.subfigures(1, 2)
1015+
fig.subplots_adjust(left=0.05, bottom=0.05, right=0.95, top=0.95)
1016+
1017+
x = np.linspace(-2, 3, 50)
1018+
y = np.linspace(-2, 3, 30)
1019+
z = np.cos(x[np.newaxis, :]) + np.sin(y[:, np.newaxis])
1020+
1021+
cmap = mpl.colormaps["viridis"]
1022+
for orientation, subfig in zip(['horizontal', 'vertical'], subfigs):
1023+
axs = subfig.subplots(2, 2).ravel()
1024+
for ax, (extend, levels, hatches) in zip(axs, params):
1025+
cs = ax.contourf(x, y, z, levels, hatches=hatches,
1026+
cmap=cmap, extend=extend)
1027+
subfig.colorbar(cs, ax=ax, orientation=orientation, fraction=0.4,
1028+
extendfrac=0.2, aspect=5)
1029+
1030+
10011031
def test_negative_boundarynorm():
10021032
fig, ax = plt.subplots(figsize=(1, 3))
10031033
cmap = mpl.colormaps["viridis"]

0 commit comments

Comments
 (0)