8000 MNT: simplify getting the first element · matplotlib/matplotlib@a023ecf · GitHub
[go: up one dir, main page]

Skip to content

Commit a023ecf

Browse files
committed
MNT: simplify getting the first element
Use `safe_first_element` from cbook which does the `next(iter(x))` dance to get the first element of an object (as defined by what you get when you iterate over it).
1 parent 10770e9 commit a023ecf

File tree

1 file changed

+4
-23
lines changed

1 file changed

+4
-23
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2138,33 +2138,14 @@ def _convert_dx(dx, x0, xconv, convert):
21382138
# removes the units from unit packages like `pint` that
21392139
# wrap numpy arrays.
21402140
try:
2141-
x0 = x0[0]
2141+
x0 = cbook.safe_first_element(x0)
21422142
except (TypeError, IndexError, KeyError):
2143-
try:
2144-
xx = np.atleast_1d(np.asarray(x0))
2145-
if len(xx) == 1:
2146-
# this keeps units for singletons...
2147-
x0 = x0
2148-
else:
2149-
# fallback for objects with length, but strange
2150-
# ways of indexing (i.e. pandas)
2151-
x0 = xx[0]
2152-
except:
2153-
# generic except. Think this should never happen, but...
2154-
x0 = x0
2143+
x0 = x0
21552144

21562145
try:
2157-
x = xconv[0]
2146+
x = cbook.safe_first_element(xconv)
21582147
except (TypeError, IndexError, KeyError):
2159-
try:
2160-
xx = np.atleast_1d(np.asarray(xconv))
2161-
if len(xx) == 1:
2162-
x = xconv
2163-
else:
2164-
x = xx[0]
2165-
except:
2166-
# generic except. Think this should never happen, but...
2167-
x = xconv
2148+
x = xconv
21682149

21692150
delist = False
21702151
if not np.iterable(dx):

0 commit comments

Comments
 (0)
0