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

Skip to content

Commit f8bc604

Browse files
committed
FIX: _safe_first_finite on all non-finite array
1 parent 161c96c commit f8bc604

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.py

Lines 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

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

610610

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

613625
class Dummy:

0 commit comments

Comments
 (0)