8000 Merge pull request #18243 from jklymak/fix-reshape-list-of-strings · matplotlib/matplotlib@17a0ff7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 17a0ff7

Browse files
authored
Merge pull request #18243 from jklymak/fix-reshape-list-of-strings
Fix reshape list of strings
2 parents 1d99132 + cd42a3f commit 17a0ff7

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

lib/matplotlib/cbook/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1377,7 +1377,10 @@ def _reshape_2D(X, name):
13771377
result = []
13781378
is_1d = True
13791379
for xi in X:
1380-
if isinstance(xi, collections.abc.Iterable):
1380+
# check if this is iterable, except for strings which we
1381+
# treat as singletons.
1382+
if (isinstance(xi, collections.abc.Iterable) and
1383+
not isinstance(xi, str)):
13811384
is_1d = False
13821385
xi = np.asanyarray(xi)
13831386
nd = np.ndim(xi)

lib/matplotlib/tests/test_category.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,10 @@ def test_overriding_units_in_plot(fig_test, fig_ref):
295295
# assert that we have not re-set the units attribute at all
296296
assert x_units is ax.xaxis.units
297297
assert y_units is ax.yaxis.units
298+
299+
300+
def test_hist():
301+
fig, ax = plt.subplots()
302+
n, bins, patches = ax.hist(['a', 'b', 'a', 'c', 'ff'])
303+
assert n.shape == (10,)
304+
np.testing.assert_allclose(n, [2., 0., 0., 1., 0., 0., 1., 0., 0., 1.])

lib/matplotlib/tests/test_cbook.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,12 @@ def __getitem__(self, item):
545545
assert len(xnew) == 1
546546
assert isinstance(xnew[0], ArraySubclass)
547547

548+
# check list of strings:
549+
x = ['a', 'b', 'c', 'c', 'dd', 'e', 'f', 'ff', 'f']
550+
xnew = cbook._reshape_2D(x, 'x')
551+
assert len(xnew[0]) == len(x)
552+
assert isinstance(xnew[0], np.ndarray)
553+
548554

549555
def test_contiguous_regions():
550556
a, b, c = 3, 4, 5

0 commit comments

Comments
 (0)
0