From efbd2cdd32077154ea278622eb52dad0b30fcbed Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Sun, 28 Jan 2024 19:08:21 -0800 Subject: [PATCH] MNT/ENH: make hist and friends more forgiving of array-like --- lib/matplotlib/cbook.py | 14 +++++++++----- lib/matplotlib/tests/test_axes.py | 10 ++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/cbook.py b/lib/matplotlib/cbook.py index ff6b2a15ec35..ea30fe67263f 100644 --- a/lib/matplotlib/cbook.py +++ b/lib/matplotlib/cbook.py @@ -1411,13 +1411,17 @@ def _reshape_2D(X, name): *name* is used to generate the error message for invalid inputs. """ - # Unpack in case of e.g. Pandas or xarray object - X = _unpack_to_numpy(X) - # Iterate over columns for ndarrays. - if isinstance(X, np.ndarray): + if hasattr(X, 'iloc'): + # probably pandas... + # return each column as a separate list element + if X.ndim == 1: + return [np.array(X)] + elif X.ndim == 2: + return [np.array(X.iloc[:, i]) for i in range(X.shape[1])] + + if hasattr(X, 'T') and hasattr(X, 'ndim'): X = X.T - if len(X) == 0: return [[]] elif X.ndim == 1 and np.ndim(X[0]) == 0: diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 530fddf127a8..6c63151e122a 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -6751,6 +6751,16 @@ def test_pandas_indexing_hist(pd): ax.hist(ser_2) +def test_pandas_hist2D(pd): + np.random.seed(19680801) + x0 = np.random.randn(200, 3) + df = pd.DataFrame(x0, columns=["a", "b", "c"]) + fig, ax = plt.subplots() + tops = ax.hist(df) + # one list of hist values for each column: + assert tops[0].shape[0] == 3 + + def test_pandas_bar_align_center(pd): # Tests fix for issue 8767 df = pd.DataFrame({'a': range(2), 'b': range(2)}) 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