Skip to content

Commit fbde951

Browse files
authored
Merge pull request #25879 from meeseeksmachine/auto-backport-of-pr-25547-on-v3.7.x
Backport PR #25547 on branch v3.7.x (FIX: `_safe_first_finite` on all non-finite array)
2 parents a99e55e + 98b3fa6 commit fbde951

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2208,19 +2208,11 @@ def _convert_dx(dx, x0, xconv, convert):
22082208
x0 = cbook._safe_first_finite(x0)
22092209
except (TypeError, IndexError, KeyError):
22102210
pass
2211-
except StopIteration:
2212-
# this means we found no finite element, fall back to first
2213-
# element unconditionally
2214-
x0 = cbook.safe_first_element(x0)
22152211

22162212
try:
22172213
x = cbook._safe_first_finite(xconv)
22182214
except (TypeError, IndexError, KeyError):
22192215
x = xconv
2220-
except StopIteration:
2221-
# this means we found no finite element, fall back to first
2222-
# element unconditionally
2223-
x = cbook.safe_first_element(xconv)
22242216

22252217
delist = False
22262218
if not np.iterable(dx):

lib/matplotlib/cbook/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,13 +1676,13 @@ def safe_first_element(obj):
16761676

16771677
def _safe_first_finite(obj, *, skip_nonfinite=True):
16781678
"""
1679-
Return the first non-None (and optionally finite) element in *obj*.
1679+
Return the first finite element in *obj* if one is available and skip_nonfinite is
1680+
True. Otherwise return the first element.
16801681
16811682
This is a method for internal use.
16821683
1683-
This is a type-independent way of obtaining the first non-None element,
1684-
supporting both index access and the iterator protocol.
1685-
The first non-None element will be obtained when skip_none is True.
1684+
This is a type-independent way of obtaining the first finite element, supporting
1685+
both index access and the iterator protocol.
16861686
"""
16871687
def safe_isfinite(val):
16881688
if val is None:
@@ -1714,7 +1714,7 @@ def safe_isfinite(val):
17141714
raise RuntimeError("matplotlib does not "
17151715
"support generators as input")
17161716
else:
1717-
return next(val for val in obj if safe_isfinite(val))
1717+
return next((val for val in obj if safe_isfinite(val)), safe_first_element(obj))
17181718

17191719

17201720
def sanitize_sequence(data):

lib/matplotlib/tests/test_cbook.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,18 @@ def test_flatiter():
609609
assert 1 == next(it)
610610

611611

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

614626
class Dummy:

0 commit comments

Comments
 (0)