From 981e0e4aa0f746a1c8a1223010a240575ad0f895 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Sun, 9 Oct 2016 02:11:56 -0400 Subject: [PATCH 1/3] TST: Add categorical tests for NumPy string arrays. --- lib/matplotlib/tests/test_category.py | 59 +++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/lib/matplotlib/tests/test_category.py b/lib/matplotlib/tests/test_category.py index aa6afff11f41..668485d76875 100644 --- a/lib/matplotlib/tests/test_category.py +++ b/lib/matplotlib/tests/test_category.py @@ -186,6 +186,65 @@ def test_plot_1d_missing(self): self.axis_test(ax.yaxis, self.dmticks, self.dmlabels, self.dmunit_data) + @cleanup + @pytest.mark.usefixtures("data") + def test_plot_bytes(self): + counts = np.array([4, 6, 5, 1]) + fig, ax = plt.subplots(ncols=3) + + ax[0].bar(self.d, counts) + + types = [v.encode('ascii') for v in self.d] + ax[1].bar(types, counts) + + types = np.array(types) + ax[2].bar(types, counts) + + fig.canvas.draw() + + # All three plots should look like the string one. + self.axis_test(ax[1].xaxis, + ax[0].xaxis.get_majorticklocs(), + lt(ax[0].xaxis.get_majorticklabels()), + ax[0].xaxis.unit_data) + self.axis_test(ax[2].xaxis, + ax[0].xaxis.get_majorticklocs(), + lt(ax[0].xaxis.get_majorticklabels()), + ax[0].xaxis.unit_data) + + @cleanup + def test_plot_numlike(self): + counts = np.array([4, 6, 5, 1]) + fig, ax = plt.subplots(ncols=4) + + types = ['1', '11', '3', '1'] + ax[0].bar(types, counts) + + types = np.array(types) + ax[1].bar(types, counts) + + types = [b'1', b'11', b'3', b'1'] + ax[2].bar(types, counts) + + types = np.array(types) + ax[3].bar(types, counts) + + fig.canvas.draw() + + # All four plots should look like the string one. + self.axis_test(ax[1].xaxis, + ax[0].xaxis.get_majorticklocs(), + lt(ax[0].xaxis.get_majorticklabels()), + ax[0].xaxis.unit_data) + self.axis_test(ax[2].xaxis, + ax[0].xaxis.get_majorticklocs(), + lt(ax[0].xaxis.get_majorticklabels()), + ax[0].xaxis.unit_data) + self.axis_test(ax[3].xaxis, + ax[0].xaxis.get_majorticklocs(), + lt(ax[0].xaxis.get_majorticklabels()), + ax[0].xaxis.unit_data) + @cleanup @pytest.mark.usefixtures("data", "missing_data") def test_plot_2d(self): From a29e26d1df62ca1b2693139ea554dc9b4d6427b5 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Sun, 9 Oct 2016 03:28:19 -0400 Subject: [PATCH 2/3] Use category converter on NumPy str/bytes also. --- lib/matplotlib/category.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/category.py b/lib/matplotlib/category.py index 0f9009bfafc0..5c252e2306fd 100644 --- a/lib/matplotlib/category.py +++ b/lib/matplotlib/category.py @@ -131,7 +131,10 @@ def _set_seq_locs(self, data, value): self.locs.append(value) value += 1 + # Connects the convertor to matplotlib units.registry[str] = StrCategoryConverter() -units.registry[bytes] = StrCategoryConverter() +units.registry[np.str_] = StrCategoryConverter() units.registry[six.text_type] = StrCategoryConverter() +units.registry[bytes] = StrCategoryConverter() +units.registry[np.bytes_] = StrCategoryConverter() From de03d4794938653a25e5366cabc282bd77604ce7 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Thu, 13 Oct 2016 04:59:44 -0400 Subject: [PATCH 3/3] TST: Parametrize new category bytes tests. --- lib/matplotlib/tests/test_category.py | 72 +++++++++------------------ 1 file changed, 24 insertions(+), 48 deletions(-) diff --git a/lib/matplotlib/tests/test_category.py b/lib/matplotlib/tests/test_category.py index 668485d76875..031611cd03c7 100644 --- a/lib/matplotlib/tests/test_category.py +++ b/lib/matplotlib/tests/test_category.py @@ -188,62 +188,38 @@ def test_plot_1d_missing(self): @cleanup @pytest.mark.usefixtures("data") - def test_plot_bytes(self): - counts = np.array([4, 6, 5, 1]) - fig, ax = plt.subplots(ncols=3) - - ax[0].bar(self.d, counts) - - types = [v.encode('ascii') for v in self.d] - ax[1].bar(types, counts) - - types = np.array(types) - ax[2].bar(types, counts) + @pytest.mark.parametrize("bars", + [['a', 'b', 'c'], + [b'a', b'b', b'c'], + np.array([b'a', b'b', b'c'])], + ids=['string list', 'bytes list', + 'bytes ndarray']) + def test_plot_bytes(self, bars): + counts = np.array([4, 6, 5]) + fig, ax = plt.subplots() + ax.bar(bars, counts) fig.canvas.draw() - # All three plots should look like the string one. - self.axis_test(ax[1].xaxis, - ax[0].xaxis.get_majorticklocs(), - lt(ax[0].xaxis.get_majorticklabels()), - ax[0].xaxis.unit_data) - self.axis_test(ax[2].xaxis, - ax[0].xaxis.get_majorticklocs(), - lt(ax[0].xaxis.get_majorticklabels()), - ax[0].xaxis.unit_data) + self.axis_test(ax.xaxis, self.dticks, self.dlabels, self.dunit_data) @cleanup - def test_plot_numlike(self): - counts = np.array([4, 6, 5, 1]) - fig, ax = plt.subplots(ncols=4) - - types = ['1', '11', '3', '1'] - ax[0].bar(types, counts) - - types = np.array(types) - ax[1].bar(types, counts) - - types = [b'1', b'11', b'3', b'1'] - ax[2].bar(types, counts) - - types = np.array(types) - ax[3].bar(types, counts) + @pytest.mark.parametrize("bars", + [['1', '11', '3'], + np.array(['1', '11', '3']), + [b'1', b'11', b'3'], + np.array([b'1', b'11', b'3'])], + ids=['string list', 'string ndarray', + 'bytes list', 'bytes ndarray']) + def test_plot_numlike(self, bars): + counts = np.array([4, 6, 5]) + fig, ax = plt.subplots() + ax.bar(bars, counts) fig.canvas.draw() - # All four plots should look like the string one. - self.axis_test(ax[1].xaxis, - ax[0].xaxis.get_majorticklocs(), - lt(ax[0].xaxis.get_majorticklabels()), - ax[0].xaxis.unit_data) - self.axis_test(ax[2].xaxis, - ax[0].xaxis.get_majorticklocs(), - lt(ax[0].xaxis.get_majorticklabels()), - ax[0].xaxis.unit_data) - self.axis_test(ax[3].xaxis, - ax[0].xaxis.get_majorticklocs(), - lt(ax[0].xaxis.get_majorticklabels()), - ax[0].xaxis.unit_data) + unitmap = MockUnitData([('1', 0), ('11', 1), ('3', 2)]) + self.axis_test(ax.xaxis, [0, 1, 2], ['1', '11', '3'], unitmap) @cleanup @pytest.mark.usefixtures("data", "missing_data")