8000 FIX: mapping for uniform-spaced boundaries on colorbars · matplotlib/matplotlib@089430c · GitHub
[go: up one dir, main page]

Skip to content

Commit 089430c

Browse files
committed
FIX: mapping for uniform-spaced boundaries on colorbars
1 parent 552e078 commit 089430c

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
@@ -1149,16 +1149,21 @@ def _mesh(self):
11491149
return (Y, 8000 X, extendlen)
11501150

11511151
def _forward_boundaries(self, x):
1152+
# map boundaries equally between 0 and 1...
11521153
b = self._boundaries
1153-
y = np.interp(x, b, np.linspace(0, b[-1], len(b)))
1154+
y = np.interp(x, b, np.linspace(0, 1, len(b)))
1155+
# the following avoids ticks in the extends:
11541156
eps = (b[-1] - b[0]) * 1e-6
1157+
# map these _well_ out of bounds to keep any ticks out
1158+
# of the extends region...
11551159
y[x < b[0]-eps] = -1
11561160
y[x > b[-1]+eps] = 2
11571161
return y
11581162

11591163
def _inverse_boundaries(self, x):
1164+
# invert the above...
11601165
b = self._boundaries
1161-
return np.interp(x, np.linspace(0, b[-1], len(b)), b)
1166+
return np.interp(x, np.linspace(0, 1, len(b)), b)
11621167

11631168
def _reset_locator_formatter_scale(self):
11641169
"""

lib/matplotlib/tests/test_colorbar.py

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