From 9ee4377df3a30a07636792cc37777e9e3e64a9c2 Mon Sep 17 00:00:00 2001 From: Kyle Sunden Date: Thu, 24 Aug 2023 21:38:44 -0500 Subject: [PATCH] Squeeze post-converted values when validating limits Closes #26596 Ensures that ndarrays can be passed to later on. In this particular case, we know that we expect a single value. --- lib/matplotlib/axes/_base.py | 2 ++ lib/matplotlib/tests/test_category.py | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 3796d9bbe508..cbc7cc0adf41 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -3562,6 +3562,8 @@ def _validate_converted_limits(self, limit, convert): """ if limit is not None: converted_limit = convert(limit) + if isinstance(converted_limit, np.ndarray): + converted_limit = converted_limit.squeeze() if (isinstance(converted_limit, Real) and not np.isfinite(converted_limit)): raise ValueError("Axis limits cannot be NaN or Inf") diff --git a/lib/matplotlib/tests/test_category.py b/lib/matplotlib/tests/test_category.py index 87dece6346f7..fd4aec88b574 100644 --- a/lib/matplotlib/tests/test_category.py +++ b/lib/matplotlib/tests/test_category.py @@ -1,4 +1,6 @@ """Catch all for categorical functions""" +import warnings + import pytest import numpy as np @@ -309,3 +311,13 @@ def test_hist(): n, bins, patches = ax.hist(['a', 'b', 'a', 'c', 'ff']) assert n.shape == (10,) np.testing.assert_allclose(n, [2., 0., 0., 1., 0., 0., 1., 0., 0., 1.]) + + +def test_set_lim(): + # Numpy 1.25 deprecated casting [2.] to float, catch_warnings added to error + # with numpy 1.25 and prior to the change from gh-26597 + # can be removed once the minimum numpy version has expired the warning + f, ax = plt.subplots() + ax.plot(["a", "b", "c", "d"], [1, 2, 3, 4]) + with warnings.catch_warnings(): + ax.set_xlim("b", "c") 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