8000 Merge pull request #21689 from meeseeksmachine/auto-backport-of-pr-21… · matplotlib/matplotlib@2b124c7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2b124c7

Browse files
authored
Merge pull request #21689 from meeseeksmachine/auto-backport-of-pr-21676-on-v3.5.x
Backport PR #21676 on branch v3.5.x (Fix boundary norm negative)
2 parents 2c69dbb + 15b7975 commit 2b124c7

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

lib/matplotlib/colorbar.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,16 +1146,21 @@ def _mesh(self):
11461146
return (Y, X, extendlen)
11471147

11481148
def _forward_boundaries(self, x):
1149+
# map boundaries equally between 0 and 1...
11491150
b = self._boundaries
1150-
y = np.interp(x, b, np.linspace(0, b[-1], len(b)))
1151+
y = np.interp(x, b, np.linspace(0, 1, len(b)))
1152+
# the following avoids ticks in the extends:
11511153
eps = (b[-1] - b[0]) * 1e-6
1154+
# map these _well_ out of bounds to keep any ticks out
1155+
# of the extends region...
11521156
y[x < b[0]-eps] = -1
11531157
y[x > b[-1]+eps] = 2
11541158
return y
11551159

11561160
def _inverse_boundaries(self, x):
1161+
# invert the above...
11571162
b = self._boundaries
1158-
return np.interp(x, np.linspace(0, b[-1], len(b)), b)
1163+
return np.interp(x, np.linspace(0, 1, len(b)), b)
11591164

11601165
def _reset_locator_formatter_scale(self):
11611166
"""

lib/matplotlib/tests/test_colorbar.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,3 +871,32 @@ def test_proportional_colorbars():
871871
CS3 = axs[i, j].contourf(X, Y, Z, levels, cmap=cmap, norm=norm,
872872
extend=extends[i])
873873
fig.colorbar(CS3, spacing=spacings[j], ax=axs[i, j])
874+
875+
876+
def test_negative_boundarynorm():
877+
fig, ax = plt.subplots(figsize=(1, 3))
878+
cmap = plt.get_cmap("viridis")
879+
880+
clevs = np.arange(-94, -85)
881+
norm = BoundaryNorm(clevs, cmap.N)
882+
cb = fig.colorbar(cm.ScalarMappable(cmap=cmap, norm=norm), cax=ax)
883+
np.testing.assert_allclose(cb.ax.get_ylim(), [clevs[0], clevs[-1]])
884+
np.testing.assert_allclose(cb.ax.get_yticks(), clevs)
885+
886+
clevs = np.arange(85, 94)
887+
norm = BoundaryNorm(clevs, cmap.N)
888+
cb = fig.colorbar(cm.ScalarMappable(cmap=cmap, norm=norm), cax=ax)
889+
np.testing.assert_allclose(cb.ax.get_ylim(), [clevs[0], clevs[-1]])
890+
np.testing.assert_allclose(cb.ax.get_yticks(), clevs)
891+
892+
clevs = np.arange(-3, 3)
893+
norm = BoundaryNorm(clevs, cmap.N)
894+
cb = fig.colorbar(cm.ScalarMappable(cmap=cmap, norm=norm), cax=ax)
895+
np.testing.assert_allclose(cb.ax.get_ylim(), [clevs[0], clevs[-1]])
896+
np.testing.assert_allclose(cb.ax.get_yticks(), clevs)
897+
898+
clevs = np.arange(-8, 1)
899+
norm = BoundaryNorm(clevs, cmap.N)
900+
cb = fig.colorbar(cm.ScalarMappable(cmap=cmap, norm=norm), cax=ax)
901+
np.testing.assert_allclose(cb.ax.get_ylim(), [clevs[0], clevs[-1]])
902+
np.testing.assert_allclose(cb.ax.get_yticks(), clevs)

lib/matplotlib/tests/test_contour.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,12 @@ def test_clabel_zorder(use_clabeltext, contour_zorder, clabel_zorder):
305305
assert clabel.get_zorder() == expected_clabel_zorder
306306

307307

308+
# tol because ticks happen to fall on pixel boundaries so small
309+
# floating point changes in tick location flip which pixel gets
310+
# the tick.
308311
@image_comparison(['contour_log_extension.png'],
309-
remove_text=True, style='mpl20')
312+
remove_text=True, style='mpl20',
313+
tol=1.444)
310314
def test_contourf_log_extension():
311315
# Remove this line when this test image is regenerated.
312316
plt.rcParams['pcolormesh.snap'] = False

0 commit comments

Comments
 (0)
0