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

Skip to content

Commit 51b55fa

Browse files
committed
Handle colorbar orientation directly in mesh generation.
1 parent 209bf11 commit 51b55fa

File tree

5 files changed

+13
-21
lines changed

5 files changed

+13
-21
lines changed

lib/matplotlib/colorbar.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -529,14 +529,13 @@ def draw_all(self):
529529
# Set self.vmin and self.vmax to first and last boundary, excluding
530530
# extensions.
531531
self.vmin, self.vmax = self._boundaries[self._inside][[0, -1]]
532-
# Compute the X/Y mesh, assuming vertical orientation.
532+
# Compute the X/Y mesh.
533533
X, Y = self._mesh()
534534
# Extract bounding polygon (the last entry's value (X[0, 1]) doesn't
535535
# matter, it just matches the CLOSEPOLY code).
536536
x = np.concatenate([X[[0, 1, -2, -1], 0], X[[-1, -2, 1, 0, 0], 1]])
537537
y = np.concatenate([Y[[0, 1, -2, -1], 0], Y[[-1, -2, 1, 0, 0], 1]])
538-
xy = (np.column_stack([x, y]) if self.orientation == 'vertical' else
539-
np.column_stack([y, x])) # Apply orientation.
538+
xy = np.column_stack([x, y])
540539
# Configure axes limits, patch, and outline.
541540
xmin, ymin = xy.min(axis=0)
542541
xmax, ymax = xy.max(axis=0)
@@ -796,13 +795,9 @@ def set_label(self, label, *, loc=None, **kwargs):
796795

797796
def _edges(self, X, Y):
798797
"""Return the separator line segments; helper for _add_solids."""
799-
N = X.shape[0]
800798
# Using the non-array form of these line segments is much
801799
# simpler than making them into arrays.
802-
if self.orientation == 'vertical':
803-
return [list(zip(X[i], Y[i])) for i in range(1, N - 1)]
804-
else:
805-
return [list(zip(Y[i], X[i])) for i in range(1, N - 1)]
800+
return [list(zip(X[i], Y[i])) for i in range(1, len(X) - 1)]
806801

807802
def _add_solids(self, X, Y, C):
808803
"""Draw the colors; optionally add separators."""
@@ -823,9 +818,8 @@ def _add_solids(self, X, Y, C):
823818

824819
def _add_solids_pcolormesh(self, X, Y, C):
825820
_log.debug('Setting pcolormesh')
826-
args = (X, Y, C) if self.orientation == 'vertical' else (Y.T, X.T, C.T)
827821
self.solids = self.ax.pcolormesh(
828-
*args, cmap=self.cmap, norm=self.norm, alpha=self.alpha,
822+
X, Y, C, cmap=self.cmap, norm=self.norm, alpha=self.alpha,
829823
edgecolors='none', shading='flat')
830824
if not self.drawedges:
831825
if len(self._y) >= self.n_rasterize:
@@ -839,8 +833,6 @@ def _add_solids_patches(self, X, Y, C, mappable):
839833
[X[i, 1], Y[i, 0]],
840834
[X[i + 1, 1], Y[i + 1, 0]],
841835
[X[i + 1, 0], Y[i + 1, 1]]])
842-
if self.orientation == 'horizontal':
843-
xy = xy[..., ::-1] # Swap x/y.
844836
patch = mpatches.PathPatch(mpath.Path(xy),
845837
facecolor=self.cmap(self.norm(C[i][0])),
846838
hatch=hatches[i], linewidth=0,
@@ -1098,10 +1090,9 @@ def _proportional_y(self):
10981090
def _mesh(self):
10991091
"""
11001092
Return the coordinate arrays for the colorbar pcolormesh/patches.
1101-
These are suitable for a vertical colorbar; swapping and transposition
1102-
for a horizontal colorbar are done outside this function.
11031093
1104-
These are scaled between vmin and vmax.
1094+
These are scaled between vmin and vmax, and already handle colorbar
1095+
orientation.
11051096
"""
11061097
# copy the norm and change the vmin and vmax to the vmin and
11071098
# vmax of the colorbar, not the norm. This allows the situation
@@ -1134,7 +1125,7 @@ def _mesh(self):
11341125
X[0, :] = xmid
11351126
if self._extend_upper() and not self.extendrect:
11361127
X[-1, :] = xmid
1137-
return X, Y
1128+
return (X, Y) if self.orientation == 'vertical' else (Y, X)
11381129

11391130
def _locate(self, x):
11401131
"""

lib/matplotlib/tests/test_colorbar.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ def test_colorbar_extension_shape():
110110

111111

112112
@image_comparison(['colorbar_extensions_uniform.png',
113-
'colorbar_extensions_proportional.png'])
113+
'colorbar_extensions_proportional.png'],
114+
tol=1.0)
114115
def test_colorbar_extension_length():
115116
"""Test variable length colorbar extensions."""
116117
# Remove this line when this test image is regenerated.

lib/matplotlib/tests/test_colors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ def test_cmap_and_norm_from_levels_and_colors():
634634

635635

636636
@image_comparison(baseline_images=['boundarynorm_and_colorbar'],
637-
extensions=['png'])
637+
extensions=['png'], tol=1.0)
638638
def test_boundarynorm_and_colorbarbase():
639639
# Remove this line when this test image is regenerated.
640640
plt.rcParams['pcolormesh.snap'] = False

lib/matplotlib/tests/test_constrainedlayout.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def test_constrained_layout4():
7676
fig.colorbar(pcm, ax=axs, pad=0.01, shrink=0.6)
7777

7878

79-
@image_comparison(['constrained_layout5.png'])
79+
@image_comparison(['constrained_layout5.png'], tol=0.002)
8080
def test_constrained_layout5():
8181
"""
8282
Test constrained_layout for a single colorbar with subplots,
@@ -93,7 +93,7 @@ def test_constrained_layout5():
9393
location='bottom')
9494

9595

96-
@image_comparison(['constrained_layout6.png'])
96+
@image_comparison(['constrained_layout6.png'], tol=0.002)
9797
def test_constrained_layout6():
9898
"""Test constrained_layout for nested gridspecs"""
9999
# Remove this line when this test image is regenerated.

lib/matplotlib/tests/test_image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ def test_image_preserve_size2():
798798
np.identity(n, bool)[::-1])
799799

800800

801-
@image_comparison(['mask_image_over_under.png'], remove_text=True)
801+
@image_comparison(['mask_image_over_under.png'], remove_text=True, tol=1.0)
802802
def test_mask_image_over_under():
803803
# Remove this line when this test image is regenerated.
804804
plt.rcParams['pcolormesh.snap'] = False

0 commit comments

Comments
 (0)