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

Skip to content

Commit 201dc2a

Browse files
committed
Initailize class 2D norm
1 parent 24de043 commit 201dc2a

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

lib/matplotlib/colors.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,60 @@ def __call__(self, value, clip=None):
13491349
def inverse(self, value):
13501350
return value
13511351

1352+
class Norm2d:
1353+
"""
1354+
Normalize a list of two values corresponding to two 1D normalizers
1355+
"""
1356+
def __init__(self, norm_instances=None):
1357+
"""
1358+
Parameters
1359+
----------
1360+
norm_instances :
1361+
A list of length two having instances of 1D normalizers
1362+
"""
1363+
if norm_instances is None:
1364+
self.norm_instances = [Normalize(), Normalize()]
1365+
else:
1366+
self.norm_instances = norm_instances
1367+
1368+
def __call__(self, values, clip=None):
1369+
"""
1370+
Parameters
1371+
----------
1372+
values : array-like
1373+
A list of two values to be normalized
1374+
clip : list of bools, None, optional
1375+
A list of two bools corresponding to value in values.
1376+
If clip is None then clip is set according to corresponding
1377+
normalizers.
1378+
1379+
Returns
1380+
-------
1381+
A list of two normalized values according to corresponding 1D
1382+
normalizers.
1383+
"""
1384+
norm_one, norm_two = self.norm_instances
1385+
1386+
if clip is None:
1387+
clip = [norm_one.clip, norm_two.clip]
1388+
1389+
return [norm_one(values[0], clip=clip[0]),
1390+
norm_two(values[1], clip=clip[1])]
1391+
1392+
def inverse(self, values):
1393+
"""
1394+
Parameters
1395+
----------
1396+
values : array-like
1397+
A list of two values to be inverted
1398+
1399+
Returns
1400+
-------
1401+
A list of two unnormalized values
1402+
"""
1403+
norm_one, norm_two = self.norm_instances
1404+
1405+
return [norm_one.inverse(values[0]), norm_two.inverse(values[1])]
13521406

13531407
def rgb_to_hsv(arr):
13541408
"""

lib/matplotlib/tests/test_colors.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,49 @@ def _mask_tester(norm_instance, vals):
272272
masked_array[0] = np.ma.masked
273273
assert_array_equal(masked_array.mask, norm_instance(masked_array).mask)
274274

275+
@pytest.mark.parametrize(
276+
'norm_instances, values, expected, clip', [
277+
(
278+
None,
279+
[[0.2, 0.4, 0.5], [5, 7, 9, 10]],
280+
[[0, 0.666667, 1], [0, 0.4, 0.8, 1]],
281+
None
282+
),
283+
(
284+
[mcolors.LogNorm(clip=True, vmax=5), mcolors.Normalize()],
285+
[[1, 6], [5, 7, 9, 10]],
286+
[[0, 1.0], [0, 0.4, 0.8, 1]],
287+
None
288+
),
289+
(
290+
[mcolors.PowerNorm(2, vmin=0, vmax=8, clip=None),
291+
mcolors.PowerNorm(2, vmin=2, vmax=8, clip=True)],
292+
[np.array([-0.5, 0, 2, 4, 8], dtype=float),
293+
np.array([-0.5, 0, 1, 8, 16], dtype=float)],
294+
[[0, 0, 1/16, 1/4, 1], [0, 0, 0, 1, 1]],
295+
None
296+
),
297+
(
298+
[mcolors.SymLogNorm(3, vmin=-30, vmax=5, linscale=1.2),
299+
mcolors.PowerNorm(2, vmin=2, vmax=8, clip=False)],
300+
[np.array([-30, -1, 2, 6], dtype=float),
301+
np.array([-0.5, 0, 1, 8, 16], dtype=float)],
302+
[[0., 0.53980074, 0.826991, 1.02758204], [0, 0, 0, 1, 1]],
303+
[False, True]
304+
)
305+
], ids=[
306+
'norm_is_None',
307+
'LogNorm_and_LinearNorm',
308+
'PowerNorm_clip_None_and_True',
309+
'SymLogNorm_and_PowerNorm_clip_in_call'
310+
]
311+
)
312+
def test_norm2d(norm_instances, values, expected, clip):
313+
norm = mcolors.Norm2d(norm_instances=norm_instances)
314+
ans1, ans2 = norm(values=values, clip=clip)
315+
assert_array_almost_equal(ans1, expected[0])
316+
assert_array_almost_equal(ans2, expected[1])
317+
275318

276319
@image_comparison(baseline_images=['levels_and_colors'],
277320
extensions=['png'])

0 commit comments

Comments
 (0)