Skip to content

Commit

Permalink
use norm explicitly (#76)
Browse files Browse the repository at this point in the history
* use norm explicitly

* remove debug statement

* add second test

* edge case when norm is string

* removed additional space

* make test into img compare

* updated warning message

* remove debug statement
  • Loading branch information
cvanelteren authored Feb 14, 2025
1 parent 1899d64 commit 8cd9a08
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
9 changes: 7 additions & 2 deletions ultraplot/axes/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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")
Expand Down Expand Up @@ -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(
Expand Down
28 changes: 28 additions & 0 deletions ultraplot/tests/test_1dplots.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8cd9a08

Please sign in to comment.