From 98b3fa698a2c12a9b4854c1924abf5042885757c Mon Sep 17 00:00:00 2001 From: Kyle Sunden Date: Fri, 12 May 2023 17:55:30 -0500 Subject: [PATCH] Backport PR #25547: FIX: `_safe_first_finite` on all non-finite array --- lib/matplotlib/axes/_axes.py | 8 -------- lib/matplotlib/cbook/__init__.py | 10 +++++----- lib/matplotlib/tests/test_cbook.py | 12 ++++++++++++ 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 39c3982cc5fa..1270de746d70 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -2208,19 +2208,11 @@ def _convert_dx(dx, x0, xconv, convert): x0 = cbook._safe_first_finite(x0) except (TypeError, IndexError, KeyError): pass - except StopIteration: - # this means we found no finite element, fall back to first - # element unconditionally - x0 = cbook.safe_first_element(x0) try: x = cbook._safe_first_finite(xconv) except (TypeError, IndexError, KeyError): x = xconv - except StopIteration: - # this means we found no finite element, fall back to first - # element unconditionally - x = cbook.safe_first_element(xconv) delist = False if not np.iterable(dx): diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index 0a6398ed40b1..37fd351de3ae 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -1676,13 +1676,13 @@ def safe_first_element(obj): def _safe_first_finite(obj, *, skip_nonfinite=True): """ - Return the first non-None (and optionally finite) element in *obj*. + Return the first finite element in *obj* if one is available and skip_nonfinite is + True. Otherwise return the first element. This is a method for internal use. - This is a type-independent way of obtaining the first non-None element, - supporting both index access and the iterator protocol. - The first non-None element will be obtained when skip_none is True. + This is a type-independent way of obtaining the first finite element, supporting + both index access and the iterator protocol. """ def safe_isfinite(val): if val is None: @@ -1714,7 +1714,7 @@ def safe_isfinite(val): raise RuntimeError("matplotlib does not " "support generators as input") else: - return next(val for val in obj if safe_isfinite(val)) + return next((val for val in obj if safe_isfinite(val)), safe_first_element(obj)) def sanitize_sequence(data): diff --git a/lib/matplotlib/tests/test_cbook.py b/lib/matplotlib/tests/test_cbook.py index aa5c999b7079..bed05695ca75 100644 --- a/lib/matplotlib/tests/test_cbook.py +++ b/lib/matplotlib/tests/test_cbook.py @@ -609,6 +609,18 @@ def test_flatiter(): assert 1 == next(it) +def test__safe_first_finite_all_nan(): + arr = np.full(2, np.nan) + ret = cbook._safe_first_finite(arr) + assert np.isnan(ret) + + +def test__safe_first_finite_all_inf(): + arr = np.full(2, np.inf) + ret = cbook._safe_first_finite(arr) + assert np.isinf(ret) + + def test_reshape2d(): class Dummy: pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy