Skip to content

Commit 4e1fbf3

Browse files
authored
use norm explicitly (#76)
* 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
1 parent 35f7c0f commit 4e1fbf3

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

ultraplot/axes/plot.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2275,7 +2275,7 @@ def _parse_cmap(
22752275
Normalize specs.
22762276
extend : optional
22772277
The colormap extend setting.
2278-
vmin, vmax : flaot, optional
2278+
vmin, vmax : float, optional
22792279
The normalization range.
22802280
sequential, diverging, cyclic, qualitative : bool, optional
22812281
Toggle various colormap types.
@@ -2295,6 +2295,12 @@ def _parse_cmap(
22952295
# Parse keyword args
22962296
cmap_kw = cmap_kw or {}
22972297
norm_kw = norm_kw or {}
2298+
# If norm is given we use it to set vmin and vmax
2299+
if (vmin is not None or vmax is not None) and norm is not None:
2300+
raise ValueError("If 'norm' is given, 'vmin' and 'vmax' must not be set.")
2301+
if isinstance(norm, mcolors.Normalize):
2302+
vmin = norm.vmin
2303+
vmax = norm.vmax
22982304
vmin = _not_none(vmin=vmin, norm_kw_vmin=norm_kw.pop("vmin", None))
22992305
vmax = _not_none(vmax=vmax, norm_kw_vmax=norm_kw.pop("vmax", None))
23002306
extend = _not_none(extend, "neither")
@@ -2440,7 +2446,6 @@ def _parse_cmap(
24402446
kwargs.update({"levels": levels, "extend": extend})
24412447
else:
24422448
guides._add_guide_kw("colorbar", kwargs, extend=extend)
2443-
24442449
return kwargs
24452450

24462451
def _parse_cycle(

ultraplot/tests/test_1dplots.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,3 +430,31 @@ def test_triplot_variants(x, y, z, triangles, use_triangulation, use_datadict):
430430
else:
431431
ax.triplot(x, y, "ko-") # Without specific triangles
432432
return fig
433+
434+
435+
@pytest.mark.mpl_image_compare
436+
def test_norm_not_modified():
437+
"""
438+
Ensure that norm is correctly passed to pcolor and related functions.
439+
"""
440+
# Create mock data and assign the colors to y
441+
# The norm should clip the data and not be modified
442+
x = np.arange(10)
443+
y = x**2
444+
c = y
445+
cmap = uplt.Colormap("viridis")
446+
norm = uplt.Norm("linear", 0, 10)
447+
fig, (left, right) = uplt.subplots(ncols=2, share=0)
448+
left.scatter(x, y, c=c, cmap=cmap, norm=norm)
449+
assert norm.vmin == 0
450+
assert norm.vmax == 10
451+
452+
arr = np.random.rand(20, 40) * 1000
453+
xe = np.linspace(0, 1, num=40, endpoint=True)
454+
ye = np.linspace(0, 1, num=20, endpoint=True)
455+
456+
norm = uplt.Norm("linear", vmin=0, vmax=1)
457+
right.pcolor(xe, ye, arr, cmap="viridis", norm=norm)
458+
assert norm.vmin == 0
459+
assert norm.vmax == 1
460+
return fig

0 commit comments

Comments
 (0)