10000 closes #7988 · story645/matplotlib@1cae86b · GitHub
[go: up one dir, main page]

Skip to content

Commit 1cae86b

Browse files
committed
1 parent 74ef9be commit 1cae86b

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

lib/matplotlib/category.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,33 @@
44"""
55
from __future__ import (absolute_import, division, print_function,
66
unicode_literals)
7-
87
import six
98

109
import numpy as np
1110

12-
import matplotlib.cbook as cbook
1311
import matplotlib.units as units
1412
import matplotlib.ticker as ticker
1513

14+
# np 1.6/1.7 support
15+
from distutils.version import LooseVersion
16+
import collections
17+
18+
19+
def shim_array(data):
20+
if LooseVersion(np.__version__) <= LooseVersion('1.8.0'):
21+
if (isinstance(data, six.string_types) or
22+
not isinstance(data, collections.Iterable)):
23+
data = [data]
24+
try:
25+
data = [str(d) for d in data]
26+
except UnicodeEncodeError:
27+
# this yields gibberish but unicode text doesn't
28+
# render under numpy1.6 anyway
29+
data = [d.encode('utf-8', 'ignore').decode('utf-8')
30+
for d in data]
31+
32+
return np.array(data, dtype=np.unicode)
33+
1634

1735
class StrCategoryConverter(units.ConversionInterface):
1836
@staticmethod
@@ -25,7 +43,8 @@ def convert(value, unit, axis):
2543
if isinstance(value, six.string_types):
2644
return vmap[value]
2745

28-
vals = np.array(value, dtype=np.unicode)
46+
vals = shim_array(value)
47+
2948
for lab, loc in vmap.items():
3049
vals[vals == lab] = loc
3150

@@ -81,8 +100,7 @@ def update(self, new_data):
81100
self._set_seq_locs(new_data, value)
82101

83102
def _set_seq_locs(self, data, value):
84-
strdata = np.array(data, dtype=np.unicode)
85-
# np.unique makes dateframes work
103+
strdata = shim_array(data)
86104
new_s = [d for d in np.unique(strdata) if d not in self.seq]
87105
for ns in new_s:
88106
self.seq.append(ns)

lib/matplotlib/tests/test_category.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
from __future__ import (absolute_import, division, print_function,
44
unicode_literals)
55

6-
from distutils.version import LooseVersion
7-
86
import pytest
97
import numpy as np
108

@@ -14,11 +12,6 @@
1412
import unittest
1513

1614

17-
needs_new_numpy = pytest.mark.xfail(
18-
LooseVersion(np.__version__) < LooseVersion('1.8.0'),
19-
reason='NumPy < 1.8.0 is broken.')
20-
21-
2215
class TestUnitData(object):
2316
testdata = [("hello world", ["hello world"], [0]),
2417
("Здравствуйте мир", ["Здравствуйте мир"], [0]),
@@ -28,14 +21,12 @@ class TestUnitData(object):
2821

2922
ids = ["single", "unicode", "mixed"]
3023

31-
@needs_new_numpy
3224
@pytest.mark.parametrize("data, seq, locs", testdata, ids=ids)
3325
def test_unit(self, data, seq, locs):
3426
act = cat.UnitData(data)
3527
assert act.seq == seq
3628
assert act.locs == locs
3729

38-
@needs_new_numpy
3930
def test_update_map(self):
4031
data = ['a', 'd']
4132
oseq = ['a', 'd']
@@ -87,7 +78,6 @@ class TestStrCategoryConverter(object):
8778
def mock_axis(self, request):
8879
self.cc = cat.StrCategoryConverter()
8980

90-
@needs_new_numpy
9181
@pytest.mark.parametrize("data, unitmap, exp", testdata, ids=ids)
9282
def test_convert(self, data, unitmap, exp):
9383
MUD = MockUnitData(unitmap)

0 commit comments

Comments
 (0)
0