Skip to content

Commit 6322731

Browse files
committed
Fix rectangle and hatches for colorbar
1 parent 9b1fcf6 commit 6322731

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

lib/matplotlib/colorbar.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -607,17 +607,19 @@ def _update_dividers(self):
607607
self.dividers.set_segments(segments)
608608

609609
def _add_solids_patches(self, X, Y, C, mappable):
610-
hatches = mappable.hatches * len(C) # Have enough hatches.
610+
hatches = mappable.hatches * (len(C) + 1) # Have enough hatches.
611611
patches = []
612+
hatch_offset = 1 if self._extend_lower() else 0
612613
for i in range(len(X) - 1):
613-
xy = np.array([[X[i, 0], Y[i, 0]],
614+
xy = np.array([[X[i, 0], Y[i, 1]],
614615
[X[i, 1], Y[i, 0]],
615616
[X[i + 1, 1], Y[i + 1, 0]],
616617
[X[i + 1, 0], Y[i + 1, 1]]])
617618
patch = mpatches.PathPatch(mpath.Path(xy),
618619
facecolor=self.cmap(self.norm(C[i][0])),
619-
hatch=hatches[i], linewidth=0,
620-
antialiased=False, alpha=self.alpha)
620+
hatch=hatches[i+hatch_offset],
621+
linewidth=0, antialiased=False,
622+
alpha=self.alpha)
621623
self.ax.add_patch(patch)
622624
patches.append(patch)
623625
self.solids_patches = patches
@@ -662,10 +664,11 @@ def _do_extends(self, ax=None):
662664
mappable = getattr(self, 'mappable', None)
663665
if (isinstance(mappable, contour.ContourSet)
664666
and any(hatch is not None for hatch in mappable.hatches)):
665-
hatches = mappable.hatches
667+
hatches = mappable.hatches * (len(self._y) + 1)
666668
else:
667-
hatches = [None]
669+
hatches = [None] * (len(self._y) + 1)
668670

671+
hatch_offset = 0
669672
if self._extend_lower():
670673
if not self.extendrect:
671674
# triangle
@@ -688,6 +691,7 @@ def _do_extends(self, ax=None):
688691
zorder=np.nextafter(self.ax.patch.zorder, -np.inf))
689692
self.ax.add_patch(patch)
690693
self._extend_patches.append(patch)
694+
hatch_offset = 1
691695
if self._extend_upper():
692696
if not self.extendrect:
693697
# triangle
@@ -700,10 +704,12 @@ def _do_extends(self, ax=None):
700704
# add the patch
701705
val = 0 if self._long_axis().get_inverted() else -1
702706
color = self.cmap(self.norm(self._values[val]))
707+
hatch_idx = len(self._y) - 1 + hatch_offset
703708
patch = mpatches.PathPatch(
704709
mpath.Path(xy), facecolor=color, alpha=self.alpha,
705710
linewidth=0, antialiased=False,
706-
transform=self.ax.transAxes, hatch=hatches[-1], clip_on=False,
711+
transform=self.ax.transAxes, hatch=hatches[hatch_idx],
712+
clip_on=False,
707713
# Place it right behind the standard patches, which is
708714
# needed if we updated the extends
709715
zorder=np.nextafter(self.ax.patch.zorder, -np.inf))

lib/matplotlib/tests/test_colorbar.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,37 @@ def test_colorbar_extend_drawedges():
979979
np.testing.assert_array_equal(cbar.dividers.get_segments(), res)
980980

981981

982+
@image_comparison(['contourf_extend_patches.png'], remove_text=True,
983+
style='mpl20')
984+
def test_colorbar_contourf_extend_patches():
985+
params = [
986+
('both', 5, ['\\', '//']),
987+
('min', 7, ['+']),
988+
('max', 2, ['|', '-', '/', '\\', '//']),
989+
('neither', 10, ['//', '\\', '||']),
990+
]
991+
992+
plt.rcParams['axes.linewidth'] = 2
993+
994+
fig = plt.figure(figsize=(10, 4))
995+
subfigs = fig.subfigures(1, 2)
996+
997+
x = np.linspace(-2, 3, 50).reshape(1, -1)
998+
y = np.linspace(-2, 3, 30).reshape(-1, 1)
999+
z = np.cos(x) + np.sin(y)
1000+
x, y = x.flatten(), y.flatten()
1001+
1002+
cmap = mpl.colormaps["viridis"]
1003+
for orientation, subfig in zip(['horizontal', 'vertical'], subfigs):
1004+
axs = subfig.subplots(2, 2).ravel()
1005+
fig.subplots_adjust(left=0.05, bottom=0.05, right=0.95, top=0.95)
1006+
for ax, (extend, levels, hatches) in zip(axs, params):
1007+
cs = ax.contourf(x, y, z, levels, hatches=hatches,
1008+
cmap=cmap, extend=extend)
1009+
fig.colorbar(cs, ax=ax, orientation=orientation, fraction=0.4,
1010+
extendfrac=0.2, aspect=5)
1011+
1012+
9821013
def test_negative_boundarynorm():
9831014
fig, ax = plt.subplots(figsize=(1, 3))
9841015
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