8000 safe_masked_invalid() for data types with multiple fields · matplotlib/matplotlib@ec11ea0 · GitHub
[go: up one dir, main page]

Skip to content

Commit ec11ea0

Browse files
committed
safe_masked_invalid() for data types with multiple fields
1 parent 74d3a63 commit ec11ea0

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/matplotlib/cbook.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,17 @@ def safe_masked_invalid(x, copy=False):
739739
try:
740740
xm = np.ma.masked_where(~(np.isfinite(x)), x, copy=False)
741741
except TypeError:
742-
return x
742+
if len(x.dtype.descr) > 1:
743+
# in case of a dtype with multiple fields:
744+
try:
745+
mask = np.empty(x.shape, dtype=np.dtype('bool, '*len(x.dtype.descr)))
746+
for dd, dm in zip(x.dtype.descr, mask.dtype.descr):
747+
mask[dm[0]] = ~(np.isfinite(x[dd[0]]))
748+
xm = np.ma.array(x, mask=mask, copy=False)
749+
except TypeError:
750+
return x
751+
else:
752+
return x
743753
return xm
744754

745755

lib/matplotlib/tests/test_multivariate_axes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,3 +571,15 @@ def test_artist_format_cursor_data_multivar():
571571
xdisp, ydisp = ax.transData.transform(v)
572572
event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp)
573573
assert im.format_cursor_data(im.get_cursor_data(event)) == text
574+
575+
576+
def test_multivariate_safe_masked_invalid():
577+
from matplotlib import cbook
578+
dt = np.dtype('float32, float32').newbyteorder('>')
579+
x = np.zeros(2, dtype=dt)
580+
x['f0'][0] = np.nan
581+
xm = cbook.safe_masked_invalid(x)
582+
assert (xm['f0'].mask == (True, False)).all()
583+
assert (xm['f1'].mask == (False, False)).all()
584+
assert '<f' in xm.dtype.descr[0][1]
585+
assert '<f' in xm.dtype.descr[1][1]

0 commit comments

Comments
 (0)
0