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

Skip to content

Commit 748ff75

Browse files
committed
created dummy axis on norm to store conversion machinary
1 parent 7eb0e8c commit 748ff75

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

lib/matplotlib/category.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,14 @@ def axisinfo(unit, axis):
7474
return munits.AxisInfo(majloc=majloc, majfmt=majfmt)
7575

7676
@staticmethod
77-
def default_units(data, axis, sort=True):
77+
def default_units(data, axis, sort=True, normed=False):
7878
"""
7979
Create mapping between string categories in *data*
80-
and integers, then store in *axis.unit_data*
80+
and integers, and store in *axis.unit_data*
8181
"""
82-
8382
if axis and axis.unit_data:
8483
axis.unit_data.update(data, sort)
85-
return
84+
return axis.unit_data
8685

8786
unit_data = UnitData(data, sort)
8887
if axis:
@@ -122,22 +121,29 @@ def __init__(self, categories):
122121
Out-of-range values are mapped to np.nan
123122
"""
124123

125-
self.unit_data = StrCategoryConverter.default_units(categories,
126-
None, sort=False)
127-
self.categories = to_str_array(categories)
128-
self.N = len(self.categories)
129-
self.nvals = self.unit_data.locs
130-
self.vmin = min(self.nvals)
131-
self.vmax = max(self.nvals)
124+
# facilitates cleaner DuckTyping of axis interface
125+
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)
132138

133139
def __call__(self, value, clip=None):
134140
# gonna have to go into imshow and undo casting
135-
value = np.asarray(value, dtype=int)
136-
ret = StrCategoryConverter.convert(value, None, self)
141+
value = np.asarray(value, dtype=np.int)
142+
ret = self.axis.units.convert(value, None, self.axis)
137143
# knock out values not in the norm
138-
mask = np.in1d(ret, self.unit_data.locs).reshape(ret.shape)
139-
# normalize ret
140-
ret /= self.vmax
144+
mask = np.in1d(ret, self.axis.unit_data.locs).reshape(ret.shape)
145+
# normalize ret & locs
146+
ret /= self.vmax
141147
return np.ma.array(ret, mask=~mask)
142148

143149

@@ -184,7 +190,7 @@ def convert_to_string(value):
184190

185191

186192
class UnitData(object):
187-
# debatable makes sense to special code missing values
193+
# debatable if it makes sense to special code missing values
188194
spdict = {'nan': -1.0, 'inf': -2.0, '-inf': -3.0}
189195

190196
def __init__(self, data, sort=True):
@@ -202,7 +208,7 @@ def __init__(self, data, sort=True):
202208
self._set_seq_locs(data, 0, sort)
203209
self.sort = sort
204210

205-
def update(self, new_data, sort=None):
211+
def update(self, new_data, sort=True):
206212
if sort:
207213
self.sort = sort
208214
# so as not to conflict with spdict

lib/matplotlib/tests/test_category.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,24 +129,23 @@ def test_StrCategoryFormatterUnicode(self):
129129

130130

131131
class TestCategoryNorm(object):
132-
testdata = [[[205, 302, 205, 101], [0, 2, 0, 1]],
133-
[[205, np.nan, 101, 305], [0, np.nan, 1, 2]],
134-
[[205, 101, 504, 101], [0, 1, np.nan, 1]]]
132+
testdata = [[[205, 302, 205, 101], [0, 1, 0, .5]],
133+
[[205, np.nan, 101, 305], [0, np.nan, .5, 1]],
134+
[[205, 101, 504, 101], [0, .5, np.nan, .5]]]
135135

136136
ids = ["regular", "nan", "exclude"]
137137

138138
@pytest.mark.parametrize("data, nmap", testdata, ids=ids)
139139
def test_norm(self, data, nmap):
140140
norm = cat.CategoryNorm([205, 101, 302])
141-
np.testing.assert_array_equal(norm(data), nmap)
141+
np.testing.assert_allclose(norm(data), nmap)
142142

143143

144144
def test_colors_from_categories():
145145
codings = {205: "red", 101: "blue", 302: "green"}
146146
cmap, norm = cat.colors_from_categories(codings)
147147
assert cmap.colors == ['red', 'green', 'blue']
148-
np.testing.assert_array_equal(norm.categories, ['205', '302', '101'])
149-
assert cmap.N == norm.N
148+
assert norm.axis.unit_data.seq == ['205', '302', '101']
150149

151150

152151
def lt(tl):

0 commit comments

Comments
 (0)