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

Skip to content

Fixed _upcast_err docstring and comments in _axes.py #22923

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

Merged
merged 1 commit into from
Apr 30, 2022
Merged
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
26 changes: 10 additions & 16 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3332,26 +3332,17 @@ def _upcast_err(err):
"""
Safely handle tuple of containers that carry units.

If the units are carried on the values then casting to object
arrays preserves the units, but if the units are on the containers
this will not work.

This function covers the case where the input to the xerr/yerr is a
length 2 tuple of equal length ndarray-subclasses that carry the
unit information in the container.

We defer coercing the units to be consistent to the underlying unit
If we have a tuple of nested numpy array (subclasses), we defer
coercing the units to be consistent to the underlying unit
library (and implicitly the broadcasting).

If we do not have a tuple of nested numpy array (subclasses),
fallback to casting to an object array.

Otherwise, fallback to casting to an object array.
"""

# we are here because we the container is not a numpy array, but it
# _is_ iterable (likely a list or a tuple but maybe something more
# exotic)

if (
# make sure it is not a scalar
np.iterable(err) and
Expand All @@ -3363,14 +3354,17 @@ def _upcast_err(err):
# fails.
isinstance(cbook.safe_first_element(err), np.ndarray)
):
# grab the type of the first element, we will try to promote
# the outer container to match the inner container
# Get the type of the first element
atype = type(cbook.safe_first_element(err))
# you can not directly pass data to the init of `np.ndarray`
# Promote the outer container to match the inner container
if atype is np.ndarray:
# Converts using np.asarray, because data cannot
# be directly passed to init of np.ndarray
return np.asarray(err, dtype=object)
# but you can for unyt and astropy uints
# If atype is not np.ndarray, directly pass data to init.
# This works for types such as unyts and astropy units
return atype(err)
# Otherwise wrap it in an object array
return np.asarray(err, dtype=object)

if xerr is not None and not isinstance(xerr, np.ndarray):
Expand Down