Skip to content

Commit 6934853

Browse files
committed
Added test for cmap.n_variates == 0 in colorizer
1 parent 9409d4c commit 6934853

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/matplotlib/colorizer.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ def _ensure_norm(norm, n_components=1):
766766
scale_cls = _api.check_getitem(scale._scale_mapping, norm=norm)
767767
return _auto_norm_from_scale(scale_cls)()
768768
return norm
769-
else: # n_components > 1
769+
elif n_components > 1:
770770
if not np.iterable(norm):
771771
_api.check_isinstance((colors.MultiNorm, None, tuple), norm=norm)
772772
if norm is None:
@@ -777,6 +777,9 @@ def _ensure_norm(norm, n_components=1):
777777
return norm
778778
raise ValueError(
779779
f"Invalid norm for multivariate colormap with {n_components} inputs")
780+
else: # n_components == 0
781+
raise ValueError(
782+
"Invalid cmap. A colorizer object must have a cmap with `n_variates` >= 1")
780783

781784

782785
def _ensure_cmap(cmap, accept_multivariate=False):

lib/matplotlib/tests/test_colors.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2199,6 +2199,16 @@ def test_colorizer_multinorm_explicit():
21992199
assert_array_almost_equal(ca.to_rgba(data), res)
22002200

22012201

2202+
def test_invalid_cmap_n_components_zero():
2203+
class CustomColormap(mcolors.Colormap):
2204+
def __init__(self):
2205+
super().__init__("custom")
2206+
self.n_variates = 0
2207+
2208+
with pytest.raises(ValueError, match='`n_variates` >= 1'):
2209+
ca = mcolorizer.Colorizer(CustomColormap())
2210+
2211+
22022212
def test_colorizer_bivar_cmap():
22032213
ca = mcolorizer.Colorizer('BiOrangeBlue', [mcolors.Normalize(), 'log'])
22042214

0 commit comments

Comments
 (0)