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

Skip to content

MNT/ENH: make hist and friends more forgiving of array-like #27714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions lib/matplotlib/cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)})
Expand Down