diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index 351a3e2fd548..f7dfa4c02b50 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -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) diff --git a/lib/matplotlib/tests/test_category.py b/lib/matplotlib/tests/test_category.py index a6d6f9afb17a..c805643bd17e 100644 --- a/lib/matplotlib/tests/test_category.py +++ b/lib/matplotlib/tests/test_category.py @@ -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.]) diff --git a/lib/matplotlib/tests/test_cbook.py b/lib/matplotlib/tests/test_cbook.py index 1c2d31a839e1..cd2684c93a01 100644 --- a/lib/matplotlib/tests/test_cbook.py +++ b/lib/matplotlib/tests/test_cbook.py @@ -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