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

Skip to content

Commit d52c2e8

Browse files
committed
FIX: be even more careful about handling yerr/yerr input
1 parent 2024498 commit d52c2e8

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3302,8 +3302,30 @@ def _upcast_err(err):
33023302
fallback to casting to an object array.
33033303
33043304
"""
3305-
if np.iterable(err) and isinstance(err[0], np.ndarray):
3306-
return type(err[0])(err)
3305+
3306+
# we are here because we the container is not a numpy array, but it
3307+
# _is_ iterable (likely a list or a tuple but maybe something more
3308+
# exotic)
3309+
3310+
if (
3311+
# make sure it is not a scalar
3312+
np.iterable(err) and
3313+
# and it is not empty
3314+
len(err) > 0 and
3315+
# and the first element is an array sub-class use
3316+
# safe_first_element because getitem is index-first not
3317+
# location first on pandas objects so err[0] almost always
3318+
# fails.
3319+
isinstance(cbook.safe_first_element(err), np.ndarray)
3320+
):
3321+
# grab the type of the first element, we will try to promote
3322+
# the outer container to match the inner container
3323+
atype = type(cbook.safe_first_element(err))
3324+
# you can not directly pass data to the init of `np.ndarray`
3325+
if atype is np.ndarray:
3326+
return np.asarray(err, dtype=object)
3327+
# but you can for unyt and astropy uints
3328+
return atype(err)
33073329
return np.asarray(err, dtype=object)
33083330

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

0 commit comments

Comments
 (0)