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)})