diff --git a/ultraplot/axes/plot.py b/ultraplot/axes/plot.py index 4a11dd6e..2ba144c7 100644 --- a/ultraplot/axes/plot.py +++ b/ultraplot/axes/plot.py @@ -2275,7 +2275,7 @@ def _parse_cmap( Normalize specs. extend : optional The colormap extend setting. - vmin, vmax : flaot, optional + vmin, vmax : float, optional The normalization range. sequential, diverging, cyclic, qualitative : bool, optional Toggle various colormap types. @@ -2295,6 +2295,12 @@ def _parse_cmap( # Parse keyword args cmap_kw = cmap_kw or {} norm_kw = norm_kw or {} + # If norm is given we use it to set vmin and vmax + if (vmin is not None or vmax is not None) and norm is not None: + raise ValueError("If 'norm' is given, 'vmin' and 'vmax' must not be set.") + if isinstance(norm, mcolors.Normalize): + vmin = norm.vmin + 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)) extend = _not_none(extend, "neither") @@ -2440,7 +2446,6 @@ def _parse_cmap( kwargs.update({"levels": levels, "extend": extend}) else: guides._add_guide_kw("colorbar", kwargs, extend=extend) - return kwargs def _parse_cycle( diff --git a/ultraplot/tests/test_1dplots.py b/ultraplot/tests/test_1dplots.py index a483c919..4a13fe8d 100644 --- a/ultraplot/tests/test_1dplots.py +++ b/ultraplot/tests/test_1dplots.py @@ -430,3 +430,31 @@ def test_triplot_variants(x, y, z, triangles, use_triangulation, use_datadict): else: ax.triplot(x, y, "ko-") # Without specific triangles return fig + + +@pytest.mark.mpl_image_compare +def test_norm_not_modified(): + """ + Ensure that norm is correctly passed to pcolor and related functions. + """ + # Create mock data and assign the colors to y + # The norm should clip the data and not be modified + x = np.arange(10) + y = x**2 + c = y + cmap = uplt.Colormap("viridis") + norm = uplt.Norm("linear", 0, 10) + fig, (left, right) = uplt.subplots(ncols=2, share=0) + left.scatter(x, y, c=c, cmap=cmap, norm=norm) + assert norm.vmin == 0 + assert norm.vmax == 10 + + arr = np.random.rand(20, 40) * 1000 + xe = np.linspace(0, 1, num=40, endpoint=True) + ye = np.linspace(0, 1, num=20, endpoint=True) + + norm = uplt.Norm("linear", vmin=0, vmax=1) + right.pcolor(xe, ye, arr, cmap="viridis", norm=norm) + assert norm.vmin == 0 + assert norm.vmax == 1 + return fig