From 91685a18f5d959dc5acb17dafa9e327e3e30331b Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Thu, 13 Aug 2020 09:29:26 -0700 Subject: [PATCH 1/3] FIX: list of strings not reshaped properly --- lib/matplotlib/cbook/__init__.py | 5 ++++- lib/matplotlib/tests/test_cbook.py | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) 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_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 From 90194cacdde15b25bc5db699e91f4a2217bb6c15 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Thu, 13 Aug 2020 09:32:07 -0700 Subject: [PATCH 2/3] FIX: list of strings not reshaped properly --- lib/matplotlib/tests/test_category.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/matplotlib/tests/test_category.py b/lib/matplotlib/tests/test_category.py index a6d6f9afb17a..f5921a69c24e 100644 --- a/lib/matplotlib/tests/test_category.py +++ b/lib/matplotlib/tests/test_category.py @@ -295,3 +295,7 @@ 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_category_hist(): + From cd42a3f944ac34eb8998100187d83393919b02e2 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Thu, 13 Aug 2020 09:45:42 -0700 Subject: [PATCH 3/3] TST: test categorical --- lib/matplotlib/tests/test_category.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_category.py b/lib/matplotlib/tests/test_category.py index f5921a69c24e..c805643bd17e 100644 --- a/lib/matplotlib/tests/test_category.py +++ b/lib/matplotlib/tests/test_category.py @@ -297,5 +297,8 @@ def test_overriding_units_in_plot(fig_test, fig_ref): assert y_units is ax.yaxis.units -def test_category_hist(): - +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.])