Skip to content

Commit f521589

Browse files
committed
Fix rectangle and hatches for colorbar
1 parent 0517187 commit f521589

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

lib/matplotlib/colorbar.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -606,17 +606,21 @@ 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]]])
616619
patch = mpatches.PathPatch(mpath.Path(xy),
617620
facecolor=self.cmap(self.norm(C[i][0])),
618-
hatch=hatches[i], linewidth=0,
619-
antialiased=False, alpha=self.alpha)
621+
hatch=hatches[i],
622+
linewidth=0, antialiased=False,
623+
alpha=self.alpha)
620624
self.ax.add_patch(patch)
621625
patches.append(patch)
622626
self.solids_patches = patches
@@ -661,9 +665,9 @@ def _do_extends(self, ax=None):
661665
mappable = getattr(self, 'mappable', None)
662666
if (isinstance(mappable, contour.ContourSet)
663667
and any(hatch is not None for hatch in mappable.hatches)):
664-
hatches = mappable.hatches
668+
hatches = mappable.hatches * (len(self._y) + 1)
665669
else:
666-
hatches = [None]
670+
hatches = [None] * (len(self._y) + 1)
667671

668672
if self._extend_lower():
669673
if not self.extendrect:
@@ -687,6 +691,8 @@ def _do_extends(self, ax=None):
687691
zorder=np.nextafter(self.ax.patch.zorder, -np.inf))
688692
self.ax.add_patch(patch)
689693
self._extend_patches.append(patch)
694+
# remove first hatch that goes into the extend patch
695+
hatches = hatches[1:]
690696
if self._extend_upper():
691697
if not self.extendrect:
692698
# triangle
@@ -699,10 +705,12 @@ def _do_extends(self, ax=None):
699705
# add the patch
700706
val = 0 if self._long_axis().get_inverted() else -1
701707
color = self.cmap(self.norm(self._values[val]))
708+
hatch_idx = len(self._y) - 1
702709
patch = mpatches.PathPatch(
703710
mpath.Path(xy), facecolor=color, alpha=self.alpha,
704711
linewidth=0, antialiased=False,
705-
transform=self.ax.transAxes, hatch=hatches[-1], clip_on=False,
712+
transform=self.ax.transAxes, hatch=hatches[hatch_idx],
713+
clip_on=False,
706714
# Place it right behind the standard patches, which is
707715
# needed if we updated the extends
708716
zorder=np.nextafter(self.ax.patch.zorder, -np.inf))

lib/matplotlib/tests/test_colorbar.py

Lines changed: 30 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy