diff --git a/ultraplot/axes/plot.py b/ultraplot/axes/plot.py index 268177adf..f456a3a93 100644 --- a/ultraplot/axes/plot.py +++ b/ultraplot/axes/plot.py @@ -641,6 +641,10 @@ Otherwise `~matplotlib.colors.Normalize` is used. norm_kw : dict-like, optional Passed to `~ultraplot.constructor.Norm`. +vcenter : float, optional + The data value at the center of a diverging normalizer. If `norm` is omitted, + selects `~ultraplot.colors.DivergingNorm`. Also accepted in `norm_kw`. + For a pre-constructed normalizer, set the center in its constructor instead. extend : {'neither', 'both', 'min', 'max'}, default: 'neither' Direction for drawing colorbar "extensions" indicating out-of-bounds data on the end of the colorbar. @@ -4171,6 +4175,7 @@ def _parse_cmap( extend=None, vmin=None, vmax=None, + vcenter=None, discrete=None, default_cmap=None, default_discrete=True, @@ -4196,6 +4201,9 @@ def _parse_cmap( The colormap extend setting. vmin, vmax : float, optional The normalization range. + vcenter : float, optional + The central value for a diverging normalizer. Selects a diverging + normalizer when `norm` is not specified. sequential, diverging, cyclic, qualitative : bool, optional Toggle various colormap types. discrete : bool, optional @@ -4243,6 +4251,18 @@ def _parse_cmap( vmax = norm.vmax vmin = _not_none(vmin=vmin, norm_kw_vmin=norm_kw.pop("vmin", None)) vmax = _not_none(vmax=vmax, norm_kw_vmax=norm_kw.pop("vmax", None)) + vcenter = _not_none( + vcenter=vcenter, norm_kw_vcenter=norm_kw.pop("vcenter", None) + ) + if vcenter is not None: + if isinstance(norm, mcolors.Normalize): + raise ValueError( + "If 'norm' is a Normalize instance, 'vcenter' must not be " + "set. Pass it through the Normalize constructor instead." + ) + norm_kw["vcenter"] = vcenter + if norm is None: + norm = "diverging" extend = _not_none(extend, "neither") modes = { key: kwargs.pop(key, None) diff --git a/ultraplot/tests/test_2dplots.py b/ultraplot/tests/test_2dplots.py index ce12b0bd1..b80f38893 100644 --- a/ultraplot/tests/test_2dplots.py +++ b/ultraplot/tests/test_2dplots.py @@ -11,18 +11,48 @@ import ultraplot as uplt, warnings -@pytest.mark.skip("not sure what this does") -@pytest.mark.mpl_image_compare -def test_colormap_vcenter(rng): - """ - Test colormap vcenter. - """ - fig, axs = uplt.subplots(ncols=3) - data = 10 * rng.random((10, 10)) - 3 - axs[0].pcolor(data, vcenter=0) - axs[1].pcolor(data, vcenter=1) - axs[2].pcolor(data, vcenter=2) - return fig +@pytest.mark.parametrize("vcenter", [0, 1, 2]) +def test_colormap_vcenter(vcenter): + """A diverging norm maps its requested center to 0.5 and limits to 0 and 1.""" + fig, ax = uplt.subplots() + data = np.array([[-3.0, 0.0], [2.0, 7.0]]) + mesh = ax.pcolor( + data, + cmap="roma", + norm="div", + discrete=False, + norm_kw={"vcenter": vcenter, "fair": False}, + vmin=-3, + vmax=7, + ) + assert mesh.norm.vcenter == vcenter + np.testing.assert_allclose(mesh.norm([-3, vcenter, 7]), [0, 0.5, 1]) + fig.canvas.draw() + + +@pytest.mark.parametrize("vcenter", [0, 1, 2]) +@pytest.mark.parametrize("discrete", [True, False]) +@pytest.mark.parametrize("via_norm_kw", [False, True]) +def test_colormap_vcenter_keyword(vcenter, discrete, via_norm_kw): + """An explicit center selects a diverging norm in either color mode.""" + fig, ax = uplt.subplots() + data = np.array([[0.0, 1.0], [2.0, 7.0]]) + kwargs = {"norm_kw": {"vcenter": vcenter}} if via_norm_kw else {"vcenter": vcenter} + mesh = ax.pcolor(data, discrete=discrete, **kwargs) + norm = mesh.norm._norm if discrete else mesh.norm + assert isinstance(norm, uplt.DivergingNorm) + assert norm.vcenter == vcenter + assert norm(vcenter) == pytest.approx(0.5) + fig.canvas.draw() + + +@pytest.mark.parametrize("via_norm_kw", [False, True]) +def test_colormap_vcenter_rejects_norm_instance(via_norm_kw): + """A separate center must not silently override an existing normalizer.""" + _, ax = uplt.subplots() + kwargs = {"norm_kw": {"vcenter": 1}} if via_norm_kw else {"vcenter": 1} + with pytest.raises(ValueError, match="Normalize instance.*vcenter"): + ax.pcolor(np.array([[0.0, 1.0], [2.0, 7.0]]), norm=Normalize(), **kwargs) @pytest.mark.mpl_image_compare