From 0313a8dbd7f4e952de73643c96fe2f653946a6de Mon Sep 17 00:00:00 2001 From: hannah Date: Sun, 7 May 2017 17:24:15 -0400 Subject: [PATCH 1/3] closes #7988 --- lib/matplotlib/category.py | 28 ++++++++++++++++++++++----- lib/matplotlib/tests/test_category.py | 10 ---------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/lib/matplotlib/category.py b/lib/matplotlib/category.py index a1ca001fe587..28b3eb3ba4d1 100644 --- a/lib/matplotlib/category.py +++ b/lib/matplotlib/category.py @@ -4,15 +4,33 @@ """ from __future__ import (absolute_import, division, print_function, unicode_literals) - import six import numpy as np -import matplotlib.cbook as cbook import matplotlib.units as units import matplotlib.ticker as ticker +# np 1.6/1.7 support +from distutils.version import LooseVersion +import collections + + +def shim_array(data): + if LooseVersion(np.__version__) <= LooseVersion('1.8.0'): + if (isinstance(data, six.string_types) or + not isinstance(data, collections.Iterable)): + data = [data] + try: + data = [str(d) for d in data] + except UnicodeEncodeError: + # this yields gibberish but unicode text doesn't + # render under numpy1.6 anyway + data = [d.encode('utf-8', 'ignore').decode('utf-8') + for d in data] + + return np.array(data, dtype=np.unicode) + class StrCategoryConverter(units.ConversionInterface): @staticmethod @@ -25,7 +43,8 @@ def convert(value, unit, axis): if isinstance(value, six.string_types): return vmap[value] - vals = np.array(value, dtype=np.unicode) + vals = shim_array(value) + for lab, loc in vmap.items(): vals[vals == lab] = loc @@ -81,8 +100,7 @@ def update(self, new_data): self._set_seq_locs(new_data, value) def _set_seq_locs(self, data, value): - strdata = np.array(data, dtype=np.unicode) - # np.unique makes dateframes work + strdata = shim_array(data) new_s = [d for d in np.unique(strdata) if d not in self.seq] for ns in new_s: self.seq.append(ns) diff --git a/lib/matplotlib/tests/test_category.py b/lib/matplotlib/tests/test_category.py index 83847e3150bb..6e5c43d76fb9 100644 --- a/lib/matplotlib/tests/test_category.py +++ b/lib/matplotlib/tests/test_category.py @@ -3,8 +3,6 @@ from __future__ import (absolute_import, division, print_function, unicode_literals) -from distutils.version import LooseVersion - import pytest import numpy as np @@ -14,11 +12,6 @@ import unittest -needs_new_numpy = pytest.mark.xfail( - LooseVersion(np.__version__) < LooseVersion('1.8.0'), - reason='NumPy < 1.8.0 is broken.') - - class TestUnitData(object): testdata = [("hello world", ["hello world"], [0]), ("Здравствуйте мир", ["Здравствуйте мир"], [0]), @@ -28,14 +21,12 @@ class TestUnitData(object): ids = ["single", "unicode", "mixed"] - @needs_new_numpy @pytest.mark.parametrize("data, seq, locs", testdata, ids=ids) def test_unit(self, data, seq, locs): act = cat.UnitData(data) assert act.seq == seq assert act.locs == locs - @needs_new_numpy def test_update_map(self): data = ['a', 'd'] oseq = ['a', 'd'] @@ -87,7 +78,6 @@ class TestStrCategoryConverter(object): def mock_axis(self, request): self.cc = cat.StrCategoryConverter() - @needs_new_numpy @pytest.mark.parametrize("data, unitmap, exp", testdata, ids=ids) def test_convert(self, data, unitmap, exp): MUD = MockUnitData(unitmap) From 92649fdba7e445f236a34a094961c3ad23f08f43 Mon Sep 17 00:00:00 2001 From: hannah Date: Mon, 8 May 2017 15:10:46 -0400 Subject: [PATCH 2/3] < not <= --- lib/matplotlib/category.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/category.py b/lib/matplotlib/category.py index 28b3eb3ba4d1..90a379d56864 100644 --- a/lib/matplotlib/category.py +++ b/lib/matplotlib/category.py @@ -17,7 +17,7 @@ def shim_array(data): - if LooseVersion(np.__version__) <= LooseVersion('1.8.0'): + if LooseVersion(np.__version__) < LooseVersion('1.8.0'): if (isinstance(data, six.string_types) or not isinstance(data, collections.Iterable)): data = [data] From cc27905006bfc57727500423491877aa9456fbe6 Mon Sep 17 00:00:00 2001 From: hannah Date: Mon, 8 May 2017 20:06:27 -0400 Subject: [PATCH 3/3] looseversion check outside function --- lib/matplotlib/category.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/category.py b/lib/matplotlib/category.py index 90a379d56864..8b64c7d6cb98 100644 --- a/lib/matplotlib/category.py +++ b/lib/matplotlib/category.py @@ -16,8 +16,11 @@ import collections -def shim_array(data): - if LooseVersion(np.__version__) < LooseVersion('1.8.0'): +if LooseVersion(np.__version__) >= LooseVersion('1.8.0'): + def shim_array(data): + return np.array(data, dtype=np.unicode) +else: + def shim_array(data): if (isinstance(data, six.string_types) or not isinstance(data, collections.Iterable)): data = [data] @@ -28,8 +31,7 @@ def shim_array(data): # render under numpy1.6 anyway data = [d.encode('utf-8', 'ignore').decode('utf-8') for d in data] - - return np.array(data, dtype=np.unicode) + return np.array(data, dtype=np.unicode) class StrCategoryConverter(units.ConversionInterface):