Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Preparations for multivariate plotting #29877

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/matplotlib/cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,17 @@
try:
xm = np.ma.masked_where(~(np.isfinite(x)), x, copy=False)
except TypeError:
return x
if len(x.dtype.descr) == 1:
return x
else:
# in case of a dtype with multiple fields:
try:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to get at least partial coverage for this branch.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't really been involved in this work nor understand how it works, but there is quite a bit of introduced code to deal with multiple datatypes? If this will be covered by tests/functionality in later PRs, that is fine, if not, please add tests for (most of) it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was asked to split #29221 into multiple PRs, and this PR is one of them.
There is tests for this functionality in #29221 using the top-level plotting functions (axes.imshow() etc.)
In my mind it is better to test using the top-level API, but if you wish I could add dedicated testing to this PR.

mask = np.empty(x.shape, dtype=np.dtype('bool, '*len(x.dtype.descr)))

Check warning on line 698 in lib/matplotlib/cbook.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/cbook.py#L697-L698

Added lines #L697 - L698 were not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the dtype be e.g. [("mask", bool, len(x.dtype.descr))] (with a slightly different API)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an interesting idea. I'll make a prototype and see if this would add unnecessary complexity somewhere else.

for dd, dm in zip(x.dtype.descr, mask.dtype.descr):
mask[dm[0]] = ~(np.isfinite(x[dd[0]]))
xm = np.ma.array(x, mask=mask, copy=False)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do numpy masked arrays actually support struct arrays as mask, with possibly different masking of the fields?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have found that this is the only way numpy supports masking dtypes with multiple fields, but I will see if [("mask", bool, len(x.dtype.descr))] as you suggest bellow is a reasonable approach to using a single mask.

except TypeError:
return x

Check warning on line 703 in lib/matplotlib/cbook.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/cbook.py#L700-L703

Added lines #L700 - L703 were not covered by tests
return xm


Expand Down
29 changes: 0 additions & 29 deletions lib/matplotlib/cm.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,32 +278,3 @@ def get_cmap(name=None, lut=None):
return _colormaps[name]
else:
return _colormaps[name].resampled(lut)


def _ensure_cmap(cmap):
"""
Ensure that we have a `.Colormap` object.

For internal use to preserve type stability of errors.

Parameters
----------
cmap : None, str, Colormap

- if a `Colormap`, return it
- if a string, look it up in mpl.colormaps
- if None, look up the default color map in mpl.colormaps

Returns
-------
Colormap

"""
if isinstance(cmap, colors.Colormap):
return cmap
cmap_name = mpl._val_or_rc(cmap, "image.cmap")
# use check_in_list to ensure type stability of the exception raised by
# the internal usage of this (ValueError vs KeyError)
if cmap_name not in _colormaps:
_api.check_in_list(sorted(_colormaps), cmap=cmap_name)
return mpl.colormaps[cmap_name]
Loading
Loading