8000 Backport PR #18243: Fix reshape list of strings · matplotlib/matplotlib@bcbabb3 · GitHub
[go: up one dir, main page]

Skip to content

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bcbabb3

Browse files
committed
Backport PR #18243: Fix reshape list of strings
1 parent 43e36f2 commit bcbabb3

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
@@ -1367,7 +1367,10 @@ def _reshape_2D(X, name):
13671367
result = []
13681368
is_1d = True
13691369
for xi in X:
1370-
if isinstance(xi, collections.abc.Iterable):
1370+
# check if this is iterable, except for strings which we
1371+
# treat as singletons.
1372+
if (isinstance(xi, collections.abc.Iterable) and
1373+
not isinstance(xi, str)):
13711374
is_1d = False
13721375
xi = np.asanyarray(xi)
13731376
nd = np.ndim(xi)

lib/matplotlib/tests/test_category.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,10 @@ def test_mixed_type_update_exception(self, ax, plotter, xdata):
269269
with pytest.raises(TypeError):
270270
plotter(ax, [0, 3], [1, 3])
271271
plotter(ax, xdata, [1, 2])
272+
273+
274+
def test_hist():
275+
fig, ax = plt.subplots()
276+
n, bins, patches = ax.hist(['a', 'b', 'a', 'c', 'ff'])
277+
assert n.shape == (10,)
278+
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