1- """
2- Tests for the colors module.
3- """
4-
51from __future__ import print_function
2+ from nose .tools import assert_raises
63import numpy as np
74from numpy .testing .utils import assert_array_equal , assert_array_almost_equal
5+
6+
87import matplotlib .colors as mcolors
98import matplotlib .cm as cm
9+ import matplotlib .pyplot as plt
10+ from matplotlib .testing .decorators import image_comparison
11+
1012
1113def test_colormap_endian ():
1214 """
@@ -23,6 +25,7 @@ def test_colormap_endian():
2325 #print(anative.dtype.isnative, aforeign.dtype.isnative)
2426 assert_array_equal (cmap (anative ), cmap (aforeign ))
2527
28+
2629def test_BoundaryNorm ():
2730 """
2831 Github issue #1258: interpolation was failing with numpy
@@ -36,7 +39,8 @@ def test_BoundaryNorm():
3639 ncolors = len (boundaries )
3740 bn = mcolors .BoundaryNorm (boundaries , ncolors )
3841 assert_array_equal (bn (vals ), expected )
39-
42+
43+
4044def test_LogNorm ():
4145 """
4246 LogNorm igornoed clip, now it has the same
@@ -46,6 +50,7 @@ def test_LogNorm():
4650 ln = mcolors .LogNorm (clip = True , vmax = 5 )
4751 assert_array_equal (ln ([1 , 6 ]), [0 , 1.0 ])
4852
53+
4954def test_Normalize ():
5055 norm = mcolors .Normalize ()
5156 vals = np .arange (- 10 , 10 , 1 , dtype = np .float )
@@ -74,6 +79,7 @@ def _inverse_tester(norm_instance, vals):
7479 """
7580 assert_array_almost_equal (norm_instance .inverse (norm_instance (vals )), vals )
7681
82+
7783def _scalar_tester (norm_instance , vals ):
7884 """
7985 Checks if scalars and arrays are handled the same way.
@@ -82,10 +88,88 @@ def _scalar_tester(norm_instance, vals):
8288 scalar_result = [norm_instance (float (v )) for v in vals ]
8389 assert_array_almost_equal (scalar_result , norm_instance (vals ))
8490
91+
8592def _mask_tester (norm_instance , vals ):
8693 """
8794 Checks mask handling
8895 """
8996 masked_array = np .ma .array (vals )
9097 masked_array [0 ] = np .ma .masked
9198 assert_array_equal (masked_array .mask , norm_instance (masked_array ).mask )
99+
100+
101+ @image_comparison (baseline_images = ['levels_and_colors' ],
102+ extensions = ['png' ])
103+ def test_cmap_and_norm_from_levels_and_colors ():
104+ data = np .linspace (- 2 , 4 , 49 ).reshape (7 , 7 )
105+ levels = [- 1 , 2 , 2.5 , 3 ]
106+ colors = ['red' , 'green' , 'blue' , 'yellow' , 'black' ]
107+ extend = 'both'
108+ cmap , norm = mcolors .from_levels_and_colors (levels , colors , extend = extend )
109+
110+ ax = plt .axes ()
111+ m = plt .pcolormesh (data , cmap = cmap , norm = norm )
112+ plt .colorbar (m )
113+
114+ # Hide the axes labels (but not the colorbar ones, as they are useful)
115+ for lab in ax .get_xticklabels () + ax .get_yticklabels ():
116+ lab .set_visible (False )
117+
118+
119+ def test_cmap_and_norm_from_levels_and_colors2 ():
120+ levels = [- 1 , 2 , 2.5 , 3 ]
121+ colors = ['red' , (0 , 1 , 0 ), 'blue' , (0.5 , 0.5 , 0.5 ), (0.0 , 0.0 , 0.0 , 1.0 )]
122+ clr = mcolors .colorConverter .to_rgba_array (colors )
123+ bad = (0.1 , 0.1 , 0.1 , 0.1 )
124+ no_color = (0.0 , 0.0 , 0.0 , 0.0 )
125+
126+ # Define the test values which are of interest.
127+ # Note: levels are lev[i] <= v < lev[i+1]
128+ tests = [('both' , None , {- 2 : clr [0 ],
129+ - 1 : clr [1 ],
130+ 2 : clr [2 ],
131+ 2.25 : clr [2 ],
132+ 3 : clr [4 ],
133+ 3.5 : clr [4 ],
134+ np .ma .array (1 , mask = True ): bad }),
135+
136+ ('min' , - 1 , {- 2 : clr [0 ],
137+ - 1 : clr [1 ],
138+ 2 : clr [2 ],
139+ 2.25 : clr [2 ],
140+ 3 : no_color ,
141+ 3.5 : no_color ,
142+ np .ma .array (1 , mask = True ): bad }),
143+
144+ ('max' , - 1 , {- 2 : no_color ,
145+ - 1 : clr [0 ],
146+ 2 : clr [1 ],
147+ 2.25 : clr [1 ],
148+ 3 : clr [3 ],
149+ 3.5 : clr [3 ],
150+ np .ma .array (1 , mask = True ): bad }),
151+
152+ ('neither' , - 2 , {- 2 : no_color ,
153+ - 1 : clr [0 ],
154+ 2 : clr [1 ],
155+ 2.25 : clr [1 ],
156+ 3 : no_color ,
157+ 3.5 : no_color ,
158+ np .ma .array (1 , mask = True ): bad }),
159+ ]
160+
161+ for extend , i1 , cases in tests :
162+ cmap , norm = mcolors .from_levels_and_colors (levels , colors [0 :i1 ],
163+ extend = extend )
164+ cmap .set_bad (bad )
165+ for d_val , expected_color in sorted (cases .items ()):
166+ assert_array_equal (expected_color , cmap (norm ([d_val ]))[0 ],
167+ 'Wih extend={0!r} and data '
168+ 'value={1!r}' .format (extend , d_val ))
169+
170+ assert_raises (ValueError , mcolors .from_levels_and_colors , levels , colors )
171+
172+
173+ if __name__ == '__main__' :
174+ import nose
175+ nose .runmodule (argv = ['-s' , '--with-doctest' ], exit = False )
0 commit comments