Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5ff0dbf

Browse files
committed
restored test_from_category_tests
1 parent 748ff75 commit 5ff0dbf

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-23
lines changed

lib/matplotlib/category.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -113,35 +113,28 @@ class CategoryNorm(mcolors.Normalize):
113113
"""
114114
Preserves ordering of discrete values
115115
"""
116-
def __init__(self, categories):
116+
def __init__(self, data):
117117
"""
118118
*categories*
119119
distinct values for mapping
120120
121121
Out-of-range values are mapped to np.nan
122122
"""
123123

124-
# facilitates cleaner DuckTyping of axis interface
124+
self.units = StrCategoryConverter()
125+
self.unit_data = None
126+
self.units.default_units(data,
127+
self, sort=False)
125128

126-
class CatAxis(object):
127-
def __init__(self):
128-
self.unit_data = None
129-
self.units = StrCategoryConverter()
130-
131-
self.axis = CatAxis()
132-
self.axis.units.default_units(categories, self.axis,
133-
sort=False)
134-
135-
nvals = self.axis.unit_data.locs
136-
self.vmin = min(nvals)
137-
self.vmax = max(nvals)
129+
self.vmin = min(self.unit_data.locs)
130+
self.vmax = max(self.unit_data.locs)
138131

139132
def __call__(self, value, clip=None):
140133
# gonna have to go into imshow and undo casting
141134
value = np.asarray(value, dtype=np.int)
142-
ret = self.axis.units.convert(value, None, self.axis)
135+
ret = self.units.convert(value, None, self)
143136
# knock out values not in the norm
144-
mask = np.in1d(ret, self.axis.unit_data.locs).reshape(ret.shape)
137+
mask = np.in1d(ret, self.unit_data.locs).reshape(ret.shape)
145138
# normalize ret & locs
146139
ret /= self.vmax
147140
return np.ma.array(ret, mask=~mask)
@@ -162,8 +155,7 @@ def colors_from_categories(codings):
162155
:class:`Normalize` instance
163156
"""
164157
if isinstance(codings, dict):
165-
codings = codings.items()
166-
158+
codings = cbook.sanitize_sequence(codings.items())
167159
values, colors = zip(*codings)
168160
cmap = mcolors.ListedColormap(list(colors))
169161
norm = CategoryNorm(list(values))

lib/matplotlib/tests/test_category.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,15 @@ def test_norm(self, data, nmap):
141141
np.testing.assert_allclose(norm(data), nmap)
142142

143143

144-
def test_colors_from_categories():
145-
codings = {205: "red", 101: "blue", 302: "green"}
146-
cmap, norm = cat.colors_from_categories(codings)
147-
assert cmap.colors == ['red', 'green', 'blue']
148-
assert norm.axis.unit_data.seq == ['205', '302', '101']
144+
class TestColorsFromCategories(object):
145+
testdata = [[{'101': "blue", '205': "red", '302': "green"}, dict],
146+
[[('205', "red"), ('101', "blue"), ('302', "green")], list]]
147+
ids = ["dict", "tuple"]
148+
149+
@pytest.mark.parametrize("codings, mtype", testdata, ids=ids)
150+
def test_colors_from_categories(self, codings, mtype):
151+
cmap, norm = cat.colors_from_categories(codings)
152+
assert mtype(zip(norm.unit_data.seq, cmap.colors)) == codings
149153

150154

151155
def lt(tl):

0 commit comments

Comments
 (0)