@@ -265,85 +265,83 @@ def test_BoundaryNorm():
265
265
vals = np .ma .masked_invalid ([np .Inf ])
266
266
assert np .all (bn (vals ).mask )
267
267
268
- # Testing extend keyword
269
- bounds = [ 1 , 2 , 3 ]
270
- cmap = cm . get_cmap ( 'jet' )
268
+ # Incompatible extend and clip
269
+ with pytest . raises ( ValueError , match = "not compatible" ):
270
+ mcolors . BoundaryNorm ( np . arange ( 4 ), 5 , extend = 'both' , clip = True )
271
271
272
- refnorm = mcolors .BoundaryNorm ([0 ] + bounds + [4 ], cmap .N )
272
+ # Too small ncolors argument
273
+ with pytest .raises (ValueError , match = "ncolors must equal or exceed" ):
274
+ mcolors .BoundaryNorm (np .arange (4 ), 2 )
275
+
276
+ with pytest .raises (ValueError , match = "ncolors must equal or exceed" ):
277
+ mcolors .BoundaryNorm (np .arange (4 ), 3 , extend = 'min' )
278
+
279
+ with pytest .raises (ValueError , match = "ncolors must equal or exceed" ):
280
+ mcolors .BoundaryNorm (np .arange (4 ), 4 , extend = 'both' )
281
+
282
+ # Testing extend keyword, with interpolation (large cmap)
283
+ bounds = [1 , 2 , 3 ]
284
+ cmap = cm .get_cmap ('viridis' )
273
285
mynorm = mcolors .BoundaryNorm (bounds , cmap .N , extend = 'both' )
274
- x = np .random .random (100 ) + 1.5
275
- np .testing .assert_array_equal (refnorm (x ), mynorm (x ))
286
+ refnorm = mcolors .BoundaryNorm ([0 ] + bounds + [4 ], cmap .N )
287
+ x = np .random .randn (100 ) * 10 + 2
288
+ ref = refnorm (x )
289
+ ref [ref == 0 ] = - 1
290
+ ref [ref == cmap .N - 1 ] = cmap .N
291
+ assert_array_equal (mynorm (x ), ref )
276
292
277
- # Min and max
293
+ # Without interpolation
278
294
cmref = mcolors .ListedColormap (['blue' , 'red' ])
279
295
cmref .set_over ('black' )
280
296
cmref .set_under ('white' )
281
-
282
297
cmshould = mcolors .ListedColormap (['white' , 'blue' , 'red' , 'black' ])
283
- cmshould .set_over (cmshould (cmshould .N ))
284
- cmshould .set_under (cmshould (0 ))
285
298
286
299
refnorm = mcolors .BoundaryNorm (bounds , cmref .N )
287
300
mynorm = mcolors .BoundaryNorm (bounds , cmshould .N , extend = 'both' )
288
- np .testing .assert_array_equal (refnorm .vmin , mynorm .vmin )
289
- np .testing .assert_array_equal (refnorm .vmax , mynorm .vmax )
290
- x = [- 1 , 1.2 , 2.3 , 9.6 ]
291
- np .testing .assert_array_equal (cmshould ([0 , 1 , 2 , 3 ]), cmshould (mynorm (x )))
292
- x = np .random .randn (100 ) * 10 + 2
293
- np .testing .assert_array_equal (cmref (refnorm (x )), cmshould (mynorm (x )))
301
+ assert mynorm .vmin == refnorm .vmin
302
+ assert mynorm .vmax == refnorm .vmax
294
303
295
- np .testing .assert_array_equal (- 1 , mynorm (- 1 ))
296
- np .testing .assert_array_equal (1 , mynorm (1.1 ))
297
- np .testing .assert_array_equal (4 , mynorm (12 ))
304
+ assert mynorm (bounds [0 ] - 0.1 ) == - 1 # under
305
+ assert mynorm (bounds [0 ] + 0.1 ) == 1 # first bin -> second color
306
+ assert mynorm (bounds [- 1 ] - 0.1 ) == cmshould .N - 2 # next-to-last color
307
+ assert mynorm (bounds [- 1 ] + 0.1 ) == cmshould .N # over
298
308
299
- # Test raises
300
- with pytest .raises (ValueError ):
301
- mcolors .BoundaryNorm (bounds , cmref .N , extend = 'both' , clip = True )
309
+ x = [- 1 , 1.2 , 2.3 , 9.6 ]
310
+ assert_array_equal (cmshould (mynorm (x )), cmshould ([0 , 1 , 2 , 3 ]))
311
+ x = np .random .randn (100 ) * 10 + 2
312
+ assert_array_equal (cmshould (mynorm (x )), cmref (refnorm (x )))
302
313
303
314
# Just min
304
315
cmref = mcolors .ListedColormap (['blue' , 'red' ])
305
316
cmref .set_under ('white' )
306
317
cmshould = mcolors .ListedColormap (['white' , 'blue' , 'red' ])
307
- cmshould .set_under (cmshould (0 ))
308
318
309
- np . testing . assert_array_equal ( 2 , cmref .N )
310
- np . testing . assert_array_equal ( 3 , cmshould .N )
319
+ assert cmref .N == 2
320
+ assert cmshould .N == 3
311
321
refnorm = mcolors .BoundaryNorm (bounds , cmref .N )
312
322
mynorm = mcolors .BoundaryNorm (bounds , cmshould .N , extend = 'min' )
313
- np . testing . assert_array_equal ( refnorm . vmin , mynorm .vmin )
314
- np . testing . assert_array_equal ( refnorm . vmax , mynorm .vmax )
323
+ assert mynorm . vmin == refnorm .vmin
324
+ assert mynorm . vmax == refnorm .vmax
315
325
x = [- 1 , 1.2 , 2.3 ]
316
- np . testing . assert_array_equal (cmshould ([0 , 1 , 2 ]), cmshould ( mynorm ( x ) ))
326
+ assert_array_equal (cmshould (mynorm ( x )), cmshould ( [0 , 1 , 2 ]))
317
327
x = np .random .randn (100 ) * 10 + 2
318
- np . testing . assert_array_equal (cmref ( refnorm (x )), cmshould ( mynorm (x )))
328
+ assert_array_equal (cmshould ( mynorm (x )), cmref ( refnorm (x )))
319
329
320
330
# Just max
321
331
cmref = mcolors .ListedColormap (['blue' , 'red' ])
322
332
cmref .set_over ('black' )
323
333
cmshould = mcolors .ListedColormap (['blue' , 'red' , 'black' ])
324
- cmshould .set_over (cmshould (2 ))
325
334
326
- np . testing . assert_array_equal ( 2 , cmref .N )
327
- np . testing . assert_array_equal ( 3 , cmshould .N )
335
+ assert cmref .N == 2
336
+ assert cmshould .N == 3
328
337
refnorm = mcolors .BoundaryNorm (bounds , cmref .N )
329
338
mynorm = mcolors .BoundaryNorm (bounds , cmshould .N , extend = 'max' )
330
- np . testing . assert_array_equal ( refnorm . vmin , mynorm .vmin )
331
- np . testing . assert_array_equal ( refnorm . vmax , mynorm .vmax )
339
+ assert mynorm . vmin == refnorm .vmin
340
+ assert mynorm . vmax == refnorm .vmax
332
341
x = [1.2 , 2.3 , 4 ]
333
- np . testing . assert_array_equal (cmshould ([0 , 1 , 2 ]), cmshould ( mynorm ( x ) ))
342
+ assert_array_equal (cmshould (mynorm ( x )), cmshould ( [0 , 1 , 2 ]))
334
343
x = np .random .randn (100 ) * 10 + 2
335
- np .testing .assert_array_equal (cmref (refnorm (x )), cmshould (mynorm (x )))
336
-
337
- # General case
338
- bounds = [1 , 2 , 3 , 4 ]
339
- cmap = cm .get_cmap ('jet' )
340
- mynorm = mcolors .BoundaryNorm (bounds , cmap .N , extend = 'both' )
341
- refnorm = mcolors .BoundaryNorm ([- 100 ] + bounds + [100 ], cmap .N )
342
- x = np .random .randn (100 ) * 10 - 5
343
- ref = refnorm (x )
344
- ref = np .where (ref == 0 , - 1 , ref )
345
- ref = np .where (ref == cmap .N - 1 , cmap .N , ref )
346
- np .testing .assert_array_equal (ref , mynorm (x ))
344
+ assert_array_equal (cmshould (mynorm (x )), cmref (refnorm (x )))
347
345
348
346
349
347
@pytest .mark .parametrize ("vmin,vmax" , [[- 1 , 2 ], [3 , 1 ]])
0 commit comments