Skip to content

Commit f7681c6

Browse files
committed
improved testing for colorizer+multinorm
1 parent 9044189 commit f7681c6

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

lib/matplotlib/cbook.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,9 +691,13 @@ def safe_masked_invalid(x, copy=False):
691691
xm = np.ma.masked_where(~(np.isfinite(x)), x, copy=False)
692692
except TypeError:
693693
if len(x.dtype.descr) == 1:
694+
# Arrays with dtype 'object' get returned here.
695+
# For example the 'c' kwarg of scatter, which supports multiple types.
696+
# `plt.scatter([3, 4], [2, 5], c=[(1, 0, 0), 'y'])`
694697
return x
695698
else:
696-
# in case of a dtype with multiple fields:
699+
# In case of a dtype with multiple fields
700+
# for example image data using a MultiNorm
697701
try:
698702
mask = np.empty(x.shape, dtype=np.dtype('bool, '*len(x.dtype.descr)))
699703
for dd, dm in zip(x.dtype.descr, mask.dtype.descr):

lib/matplotlib/colorizer.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ def _get_colorizer(cmap, norm, colorizer):
642642
cmap : str or `~matplotlib.colors.Colormap`, default: :rc:`image.cmap`
643643
The Colormap instance or registered colormap name used to map scalar data
644644
to colors.""",
645-
multi_cmap_doc="""\
645+
multi_cmap_doc="""\
646646
cmap : str, `~matplotlib.colors.Colormap`, `~matplotlib.colors.BivarColormap`\
647647
or `~matplotlib.colors.MultivarColormap`, default: :rc:`image.cmap`
648648
The Colormap instance or registered colormap name used to map
@@ -911,9 +911,6 @@ def _ensure_multivariate_data(data, n_components):
911911
reconstructed[f][data[i].mask] = np.ma.masked
912912
return reconstructed
913913

914-
if data is None:
915-
return data
916-
917914
if n_components == 1:
918915
# PIL.Image gets passed here
919916
return data

lib/matplotlib/tests/test_colors.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2195,3 +2195,34 @@ def test_colorizer_multinorm_explicit():
21952195
data = [0.1, 0.2]
21962196
res = (0.100098, 0.375492, 0.650879, 1.)
21972197
assert_array_almost_equal(ca.to_rgba(data), res)
2198+
2199+
2200+
def test_colorizer_bivar_cmap():
2201+
ca = mcolorizer.Colorizer('BiOrangeBlue', [mcolors.Normalize(), 'log'])
2202+
2203+
with pytest.raises(ValueError, match='The colormap viridis'):
2204+
ca.cmap = 'viridis'
2205+
2206+
cartist = mcolorizer.ColorizingArtist(ca)
2207+
cartist.set_array(np.zeros((2, 4, 4)))
2208+
2209+
with pytest.raises(ValueError, match='Invalid data entry for multivariate'):
2210+
cartist.set_array(np.zeros((3, 4, 4)))
2211+
2212+
dt = np.dtype([('x', 'f4'), ('', 'object')])
2213+
with pytest.raises(TypeError, match='converted to a sequence of floats'):
2214+
cartist.set_array(np.zeros((2, 4, 4), dtype=dt))
2215+
2216+
with pytest.raises(ValueError, match='all variates must have same shape'):
2217+
cartist.set_array((np.zeros(3), np.zeros(4)))
2218+
2219+
# ensure masked value is propagated from input
2220+
a = np.arange(3)
2221+
cartist.set_array((a, np.ma.masked_where(a > 1, a)))
2222+
assert cartist.get_array()['f0'].mask[2] is np.False_
2223+
assert cartist.get_array()['f1'].mask[2] is np.True_
2224+
assert cartist.get_array()['f1'].mask[1] is np.False_
2225+
2226+
# test clearing data
2227+
cartist.set_array(None)
2228+
cartist.get_array() is None

lib/matplotlib/tests/test_image.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ def test_format_cursor_data_multinorm(data, text):
463463
from matplotlib.backend_bases import MouseEvent
464464
fig, ax = plt.subplots()
465465
cmap_bivar = mpl.bivar_colormaps['BiOrangeBlue']
466+
cmap_multivar = mpl.multivar_colormaps['2VarAddA']
466467

467468
# This is a test for ColorizingArtist._format_cursor_data_override()
468469
# with data with multiple channels.
@@ -483,6 +484,10 @@ def test_format_cursor_data_multinorm(data, text):
483484
event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp)
484485
assert im.format_cursor_data(im.get_cursor_data(event)) == text
485486

487+
im.colorizer._cmap = cmap_multivar
488+
event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp)
489+
assert im.format_cursor_data(im.get_cursor_data(event)) == text
490+
486491

487492
@image_comparison(['image_clip'], style='mpl20')
488493
def test_image_clip():

0 commit comments

Comments
 (0)