Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b61bb0b

Browse filesBrowse files
authored
Merge pull request #25547 from rcomer/safe_first_finite
FIX: `_safe_first_finite` on all non-finite array
2 parents a179199 + f8bc604 commit b61bb0b
Copy full SHA for b61bb0b

File tree

Expand file treeCollapse file tree

3 files changed

+17
-13
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+17
-13
lines changed

‎lib/matplotlib/axes/_axes.py

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_axes.py
-8Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2243,19 +2243,11 @@ def _convert_dx(dx, x0, xconv, convert):
22432243
x0 = cbook._safe_first_finite(x0)
22442244
except (TypeError, IndexError, KeyError):
22452245
pass
2246-
except StopIteration:
2247-
# this means we found no finite element, fall back to first
2248-
# element unconditionally
2249-
x0 = cbook.safe_first_element(x0)
22502246

22512247
try:
22522248
x = cbook._safe_first_finite(xconv)
22532249
except (TypeError, IndexError, KeyError):
22542250
x = xconv
2255-
except StopIteration:
2256-
# this means we found no finite element, fall back to first
2257-
# element unconditionally
2258-
x = cbook.safe_first_element(xconv)
22592251

22602252
delist = False
22612253
if not np.iterable(dx):

‎lib/matplotlib/cbook.py

Copy file name to clipboardExpand all lines: lib/matplotlib/cbook.py
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,13 +1619,13 @@ def safe_first_element(obj):
16191619

16201620
def _safe_first_finite(obj, *, skip_nonfinite=True):
16211621
"""
1622-
Return the first non-None (and optionally finite) element in *obj*.
1622+
Return the first finite element in *obj* if one is available and skip_nonfinite is
1623+
True. Otherwise return the first element.
16231624
16241625
This is a method for internal use.
16251626
1626-
This is a type-independent way of obtaining the first non-None element,
1627-
supporting both index access and the iterator protocol.
1628-
The first non-None element will be obtained when skip_none is True.
1627+
This is a type-independent way of obtaining the first finite element, supporting
1628+
both index access and the iterator protocol.
16291629
"""
16301630
def safe_isfinite(val):
16311631
if val is None:
@@ -1657,7 +1657,7 @@ def safe_isfinite(val):
16571657
raise RuntimeError("matplotlib does not "
16581658
"support generators as input")
16591659
else:
1660-
return next(val for val in obj if safe_isfinite(val))
1660+
return next((val for val in obj if safe_isfinite(val)), safe_first_element(obj))
16611661

16621662

16631663
def sanitize_sequence(data):

‎lib/matplotlib/tests/test_cbook.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_cbook.py
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,18 @@ def test_flatiter():
611611
assert 1 == next(it)
612612

613613

614+
def test__safe_first_finite_all_nan():
615+
arr = np.full(2, np.nan)
616+
ret = cbook._safe_first_finite(arr)
617+
assert np.isnan(ret)
618+
619+
620+
def test__safe_first_finite_all_inf():
621+
arr = np.full(2, np.inf)
622+
ret = cbook._safe_first_finite(arr)
623+
assert np.isinf(ret)
624+
625+
614626
def test_reshape2d():
615627

616628
class Dummy:

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.