Skip to content

Fix reshape list of strings #18243

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

Merged
merged 3 commits into from
Aug 13, 2020
Merged
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
5 changes: 4 additions & 1 deletion lib/matplotlib/cbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1377,7 +1377,10 @@ def _reshape_2D(X, name):
result = []
is_1d = True
for xi in X:
if isinstance(xi, collections.abc.Iterable):
# check if this is iterable, except for strings which we
# treat as singletons.
if (isinstance(xi, collections.abc.Iterable) and
not isinstance(xi, str)):
is_1d = False
xi = np.asanyarray(xi)
nd = np.ndim(xi)
Expand Down
7 changes: 7 additions & 0 deletions lib/matplotlib/tests/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,10 @@ def test_overriding_units_in_plot(fig_test, fig_ref):
# assert that we have not re-set the units attribute at all
assert x_units is ax.xaxis.units
assert y_units is ax.yaxis.units


def test_hist():
fig, ax = plt.subplots()
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.])
6 changes: 6 additions & 0 deletions lib/matplotlib/tests/test_cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,12 @@ def __getitem__(self, item):
assert len(xnew) == 1
assert isinstance(xnew[0], ArraySubclass)

# check list of strings:
x = ['a', 'b', 'c', 'c', 'dd', 'e', 'f', 'ff', 'f']
xnew = cbook._reshape_2D(x, 'x')
assert len(xnew[0]) == len(x)
assert isinstance(xnew[0], np.ndarray)


def test_contiguous_regions():
a, b, c = 3, 4, 5
Expand Down