Skip to content

Commit 7d54509

Browse files
authored
Merge branch 'main' into feat/treemap
2 parents 60d0f91 + aff3f19 commit 7d54509

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

‎ultraplot/axes/plot.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5055,8 +5055,8 @@ def _parse_level_norm(
50555055
unique = "neither"
50565056
elif under is not None: # turn off under-bounds unique bin
50575057
if extend == "both":
5058-
unique = "min"
5059-
elif extend == "max":
5058+
unique = "max"
5059+
elif extend == "min":
50605060
unique = "neither"
50615061

50625062
# Generate DiscreteNorm for filled-contour style bins. For line contours

‎ultraplot/tests/test_2dplots.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,41 @@ def test_colormap_vcenter_rejects_norm_instance(via_norm_kw):
5555
ax.pcolor(np.array([[0.0, 1.0], [2.0, 7.0]]), norm=Normalize(), **kwargs)
5656

5757

58+
@pytest.mark.parametrize("method", ["contourf", "pcolormesh"])
59+
@pytest.mark.parametrize("cmap_name", ["RdBu", "fire"])
60+
@pytest.mark.parametrize("extend", ["neither", "min", "max", "both"])
61+
@pytest.mark.parametrize(
62+
("under", "over"),
63+
[(None, None), ("black", None), (None, "green"), ("black", "green")],
64+
)
65+
def test_colormap_extreme_colors_reserve_correct_end(
66+
method, cmap_name, extend, under, over
67+
):
68+
"""Custom extremes free their own endpoint without consuming the opposite one."""
69+
cmap = uplt.Colormap(cmap_name).with_extremes(under=under, over=over)
70+
samples = np.linspace(0, 1, 256)
71+
original_colors = cmap(samples).copy()
72+
levels = np.linspace(-1, 1, 7)
73+
centers = 0.5 * (levels[:-1] + levels[1:])
74+
data = np.tile(np.linspace(-1.5, 1.5, 9), (9, 1))
75+
_, ax = uplt.subplots()
76+
mappable = getattr(ax, method)(data, cmap=cmap, levels=levels, extend=extend)
77+
78+
# An automatic extension must remain distinct from the adjacent normal bin.
79+
# A custom extension lets that normal bin use the full colormap endpoint.
80+
for index, custom, extensions, endpoint in (
81+
(0, under, ("min", "both"), 0.0),
82+
(-1, over, ("max", "both"), 1.0),
83+
):
84+
uses_endpoint = np.allclose(mappable.to_rgba(centers[index]), cmap(endpoint))
85+
reserved = custom is None and extend in extensions
86+
assert uses_endpoint == (not reserved)
87+
88+
np.testing.assert_allclose(mappable.to_rgba(-2.0), cmap.get_under())
89+
np.testing.assert_allclose(mappable.to_rgba(2.0), cmap.get_over())
90+
np.testing.assert_array_equal(mappable.cmap(samples), original_colors)
91+
92+
5893
@pytest.mark.mpl_image_compare
5994
def test_auto_diverging1(rng):
6095
"""

0 commit comments

Comments
 (0)