16
16
list of color specifications.
17
17
18
18
The module also provides a single instance, *colorConverter*, of the
19
- :class:`ColorConverter` class providing methods for converting single
20
- color specifications or sequences of them to *RGB* or *RGBA*.
19
+ :class:`ColorConverter` class providing methods for converting single color
20
+ specifications or sequences of them to *RGB* or *RGBA*.
21
21
22
- Commands which take color arguments can use several formats to specify
23
- the colors. For the basic builtin colors, you can use a single letter
22
+ Commands which take color arguments can use several formats to specify the
23
+ colors. For the basic builtin colors, you can use a single letter
24
24
25
25
- b: blue
26
26
- g: green
31
31
- k: black
32
32
- w: white
33
33
34
- Gray shades can be given as a string encoding a float in the 0-1
35
- range, e.g.::
34
+ Gray shades can be given as a string encoding a float in the 0-1 range, e.g.::
36
35
37
36
color = '0.75'
38
37
39
- For a greater range of colors, you have two options. You can specify
40
- the color using an html hex string, as in::
38
+ For a greater range of colors, you have two options. You can specify the
39
+ color using an html hex string, as in::
41
40
42
41
color = '#eeefff'
43
42
44
- or you can pass an *R* , *G* , *B* tuple, where each of *R* , *G* , *B*
45
- are in the range [0,1].
43
+ or you can pass an *R* , *G* , *B* tuple, where each of *R* , *G* , *B* are in
44
+ the range [0,1].
46
45
47
- Finally, legal html names for colors, like 'red', 'burlywood' and
48
- 'chartreuse' are supported.
46
+ Finally, legal html names for colors, like 'red', 'burlywood' and 'chartreuse'
47
+ are supported.
49
48
"""
50
49
from __future__ import print_function , division
51
50
import re
198
197
'white' : '#FFFFFF' ,
199
198
'whitesmoke' : '#F5F5F5' ,
200
199
'yellow' : '#FFFF00' ,
201
- 'yellowgreen' : '#9ACD32' ,
202
- }
200
+ 'yellowgreen' : '#9ACD32' }
203
201
204
202
205
203
# add british equivs
@@ -255,8 +253,7 @@ class ColorConverter(object):
255
253
'm' : (0.75 , 0 , 0.75 ),
256
254
'y' : (0.75 , 0.75 , 0 ),
257
255
'k' : (0.0 , 0.0 , 0.0 ),
258
- 'w' : (1.0 , 1.0 , 1.0 ),
259
- }
256
+ 'w' : (1.0 , 1.0 , 1.0 ), }
260
257
261
258
cache = {}
262
259
@@ -286,8 +283,8 @@ def to_rgb(self, arg):
286
283
pass
287
284
except TypeError :
288
285
raise ValueError (
289
- 'to_rgb: arg "%s" is unhashable even inside a tuple'
290
- % (str (arg ),))
286
+ 'to_rgb: arg "%s" is unhashable even inside a tuple'
287
+ % (str (arg ),))
291
288
292
289
try :
293
290
if cbook .is_string_like (arg ):
@@ -301,26 +298,26 @@ def to_rgb(self, arg):
301
298
fl = float (argl )
302
299
if fl < 0 or fl > 1 :
303
300
raise ValueError (
304
- 'gray (string) must be in range 0-1' )
301
+ 'gray (string) must be in range 0-1' )
305
302
color = tuple ([fl ] * 3 )
306
303
elif cbook .iterable (arg ):
307
304
if len (arg ) > 4 or len (arg ) < 3 :
308
305
raise ValueError (
309
- 'sequence length is %d; must be 3 or 4' % len (arg ))
306
+ 'sequence length is %d; must be 3 or 4' % len (arg ))
310
307
color = tuple (arg [:3 ])
311
308
if [x for x in color if (float (x ) < 0 ) or (x > 1 )]:
312
309
# This will raise TypeError if x is not a number.
313
310
raise ValueError (
314
- 'number in rbg sequence outside 0-1 range' )
311
+ 'number in rbg sequence outside 0-1 range' )
315
312
else :
316
313
raise ValueError (
317
- 'cannot convert argument to rgb sequence' )
314
+ 'cannot convert argument to rgb sequence' )
318
315
319
316
self .cache [arg ] = color
320
317
321
318
except (KeyError , ValueError , TypeError ) as exc :
322
319
raise ValueError (
323
- 'to_rgb: Invalid rgb arg "%s"\n %s' % (str (arg ), exc ))
320
+ 'to_rgb: Invalid rgb arg "%s"\n %s' % (str (arg ), exc ))
324
321
# Error messages could be improved by handling TypeError
325
322
# separately; but this should be rare and not too hard
326
323
# for the user to figure out as-is.
@@ -348,7 +345,7 @@ def to_rgba(self, arg, alpha=None):
348
345
if [x for x in arg if (float (x ) < 0 ) or (x > 1 )]:
349
346
# This will raise TypeError if x is not a number.
350
347
raise ValueError (
351
- 'number in rbga sequence outside 0-1 range' )
348
+ 'number in rbga sequence outside 0-1 range' )
352
349
if alpha is None :
353
350
return tuple (arg )
354
351
if alpha < 0.0 or alpha > 1.0 :
@@ -357,15 +354,15 @@ def to_rgba(self, arg, alpha=None):
357
354
r , g , b = arg [:3 ]
358
355
if [x for x in (r , g , b ) if (float (x ) < 0 ) or (x > 1 )]:
359
356
raise ValueError (
360
- 'number in rbg sequence outside 0-1 range' )
357
+ 'number in rbg sequence outside 0-1 range' )
361
358
else :
362
359
r , g , b = self .to_rgb (arg )
363
360
if alpha is None :
364
361
alpha = 1.0
365
362
return r , g , b , alpha
366
363
except (TypeError , ValueError ) as exc :
367
364
raise ValueError (
368
- 'to_rgba: Invalid rgba arg "%s"\n %s' % (str (arg ), exc ))
365
+ 'to_rgba: Invalid rgba arg "%s"\n %s' % (str (arg ), exc ))
369
366
370
367
def to_rgba_array (self , c , alpha = None ):
371
368
"""
@@ -458,18 +455,18 @@ def makeMappingArray(N, data, gamma=1.0):
458
455
459
456
if x [0 ] != 0. or x [- 1 ] != 1.0 :
460
457
raise ValueError (
461
- "data mapping points must start with x=0. and end with x=1" )
458
+ "data mapping points must start with x=0. and end with x=1" )
462
459
if np .sometrue (np .sort (x ) - x ):
463
460
raise ValueError (
464
- "data mapping points must have x in increasing order" )
461
+ "data mapping points must have x in increasing order" )
465
462
# begin generation of lookup table
466
463
x = x * (N - 1 )
467
464
lut = np .zeros ((N ,), np .float )
468
465
xind = (N - 1 ) * np .linspace (0 , 1 , N ) ** gamma
469
466
ind = np .searchsorted (x , xind )[1 :- 1 ]
470
467
471
- lut [1 :- 1 ] = (((xind [1 :- 1 ] - x [ind - 1 ]) / (x [ind ] - x [ind - 1 ]))
472
- * (y0 [ind ] - y1 [ind - 1 ]) + y1 [ind - 1 ])
468
+ lut [1 :- 1 ] = (((xind [1 :- 1 ] - x [ind - 1 ]) / (x [ind ] - x [ind - 1 ])) *
469
+ (y0 [ind ] - y1 [ind - 1 ]) + y1 [ind - 1 ])
473
470
lut [0 ] = y1 [0 ]
474
471
lut [- 1 ] = y0 [- 1 ]
475
472
# ensure that the lut is confined to values between 0 and 1 by clipping it
@@ -635,8 +632,8 @@ def _init(self):
635
632
def is_gray (self ):
636
633
if not self ._isinit :
637
634
self ._init ()
638
- return (np .alltrue (self ._lut [:, 0 ] == self ._lut [:, 1 ])
639
- and np .alltrue (self ._lut [:, 0 ] == self ._lut [:, 2 ]))
635
+ return (np .alltrue (self ._lut [:, 0 ] == self ._lut [:, 1 ]) and
636
+ np .alltrue (self ._lut [:, 0 ] == self ._lut [:, 2 ]))
640
637
641
638
642
639
class LinearSegmentedColormap (Colormap ):
@@ -701,15 +698,15 @@ def __init__(self, name, segmentdata, N=256, gamma=1.0):
701
698
702
699
def _init (self ):
703
700
self ._lut = np .ones ((self .N + 3 , 4 ), np .float )
704
- self ._lut [:- 3 , 0 ] = makeMappingArray (self . N ,
705
- self ._segmentdata ['red' ], self ._gamma )
706
- self ._lut [:- 3 , 1 ] = makeMappingArray (self . N ,
707
- self ._segmentdata ['green' ], self ._gamma )
708
- self ._lut [:- 3 , 2 ] = makeMappingArray (self . N ,
709
- self ._segmentdata ['blue' ], self ._gamma )
701
+ self ._lut [:- 3 , 0 ] = makeMappingArray (
702
+ self . N , self ._segmentdata ['red' ], self ._gamma )
703
+ self ._lut [:- 3 , 1 ] = makeMappingArray (
704
+ self . N , self ._segmentdata ['green' ], self ._gamma )
705
+ self ._lut [:- 3 , 2 ] = makeMappingArray (
706
+ self . N , self ._segmentdata ['blue' ], self ._gamma )
710
707
if 'alpha' in self ._segmentdata :
711
- self ._lut [:- 3 , 3 ] = makeMappingArray (self . N ,
712
- self ._segmentdata ['alpha' ], 1 )
708
+ self ._lut [:- 3 , 3 ] = makeMappingArray (
709
+ self . N , self ._segmentdata ['alpha' ], 1 )
713
710
self ._isinit = True
714
711
self ._set_extremes ()
715
712
@@ -955,7 +952,7 @@ def __call__(self, value, clip=None):
955
952
if clip :
956
953
mask = ma .getmask (result )
957
954
result = ma .array (np .clip (result .filled (vmax ), vmin , vmax ),
958
- mask = mask )
955
+ mask = mask )
959
956
# in-place equivalent of above can be much faster
960
957
resdat = result .data
961
958
mask = result .mask
@@ -1000,7 +997,7 @@ def autoscale_None(self, A):
1000
997
self .vmin = ma .min (A )
1001
998
if self .vmax is None :
1002
999
self .vmax = ma .max (A )
1003
-
1000
+
1004
1001
1005
1002
class SymLogNorm (Normalize ):
1006
1003
"""
@@ -1039,7 +1036,7 @@ def __call__(self, value, clip=None):
1039
1036
result , is_scalar = self .process_value (value )
1040
1037
self .autoscale_None (result )
1041
1038
vmin , vmax = self .vmin , self .vmax
1042
-
1039
+
1043
1040
if vmin > vmax :
1044
1041
raise ValueError ("minvalue must be less than or equal to maxvalue" )
1045
1042
elif vmin == vmax :
@@ -1048,7 +1045,7 @@ def __call__(self, value, clip=None):
1048
1045
if clip :
1049
1046
mask = ma .getmask (result )
1050
1047
result = ma .array (np .clip (result .filled (vmax ), vmin , vmax ),
1051
- mask = mask )
1048
+ mask = mask )
1052
1049
# in-place equivalent of above can be much faster
1053
1050
resdat = self ._transform (result .data )
1054
1051
resdat -= self ._lower
@@ -1057,8 +1054,8 @@ def __call__(self, value, clip=None):
1057
1054
if is_scalar :
1058
1055
result = result [0 ]
1059
1056
return result
1060
-
1061
- def _transform (self , a ):
1057
+
1058
+ def _transform (self , a ):
1062
1059
"""
1063
1060
Inplace transformation.
1064
1061
"""
@@ -1069,7 +1066,7 @@ def _transform(self, a):
1069
1066
a [masked ] = log
1070
1067
a [~ masked ] *= self ._linscale_adj
1071
1068
return a
1072
-
1069
+
1073
1070
def _inv_transform (self , a ):
1074
1071
"""
1075
1072
Inverse inplace Transformation.
@@ -1081,31 +1078,30 @@ def _inv_transform(self, a):
1081
1078
a [masked ] = exp
1082
1079
a [~ masked ] /= self ._linscale_adj
1083
1080
return a
1084
-
1081
+
1085
1082
def _transform_vmin_vmax (self ):
1086
1083
"""
1087
1084
Calculates vmin and vmax in the transformed system.
1088
1085
"""
1089
1086
vmin , vmax = self .vmin , self .vmax
1090
1087
arr = np .array ([vmax , vmin ])
1091
- self ._upper , self ._lower = self ._transform (arr )
1092
-
1088
+ self ._upper , self ._lower = self ._transform (arr )
1093
1089
1094
1090
def inverse (self , value ):
1095
1091
if not self .scaled ():
1096
1092
raise ValueError ("Not invertible until scaled" )
1097
1093
val = ma .asarray (value )
1098
1094
val = val * (self ._upper - self ._lower ) + self ._lower
1099
1095
return self ._inv_transform (val )
1100
-
1096
+
1101
1097
def autoscale (self , A ):
1102
1098
"""
1103
1099
Set *vmin*, *vmax* to min, max of *A*.
1104
- """
1100
+ """
1105
1101
self .vmin = ma .min (A )
1106
1102
self .vmax = ma .max (A )
1107
1103
self ._transform_vmin_vmax ()
1108
-
1104
+
1109
1105
def autoscale_None (self , A ):
1110
1106
""" autoscale only None-valued vmin or vmax """
1111
1107
if self .vmin is not None and self .vmax is not None :
@@ -1355,8 +1351,8 @@ def shade_rgb(self, rgb, elevation, fraction=1.):
1355
1351
dx , dy = np .gradient (elevation )
1356
1352
slope = 0.5 * np .pi - np .arctan (np .hypot (dx , dy ))
1357
1353
aspect = np .arctan2 (dx , dy )
1358
- intensity = np .sin (alt ) * np .sin (slope ) + np .cos (alt ) * \
1359
- np .cos (slope ) * np .cos (- az - aspect - 0.5 * np .pi )
1354
+ intensity = ( np .sin (alt ) * np .sin (slope ) + np .cos (alt ) *
1355
+ np .cos (slope ) * np .cos (- az - aspect - 0.5 * np .pi ) )
1360
1356
# rescale to interval -1,1
1361
1357
# +1 means maximum sun exposure and -1 means complete shade.
1362
1358
intensity = (intensity - intensity .min ()) / \
@@ -1367,28 +1363,26 @@ def shade_rgb(self, rgb, elevation, fraction=1.):
1367
1363
hsv = rgb_to_hsv (rgb [:, :, 0 :3 ])
1368
1364
# modify hsv values to simulate illumination.
1369
1365
1370
- hsv [:, :, 1 ] = np .where (np .logical_and (
1371
- np .abs (hsv [:, :, 1 ]) > 1.e-10 ,
1372
- intensity > 0 ),
1373
- (1. - intensity ) * hsv [:, :, 1 ] +
1374
- intensity * self .hsv_max_sat ,
1366
+ hsv [:, :, 1 ] = np .where (np .logical_and (np .abs (hsv [:, :, 1 ]) > 1.e-10 ,
1367
+ intensity > 0 ),
1368
+ ((1. - intensity ) * hsv [:, :, 1 ] +
1369
+ intensity * self .hsv_max_sat ),
1375
1370
hsv [:, :, 1 ])
1376
1371
1377
1372
hsv [:, :, 2 ] = np .where (intensity > 0 ,
1378
- (1. - intensity ) * hsv [:, :, 2 ] +
1379
- intensity * self .hsv_max_val ,
1373
+ (( 1. - intensity ) * hsv [:, :, 2 ] +
1374
+ intensity * self .hsv_max_val ) ,
1380
1375
hsv [:, :, 2 ])
1381
1376
1382
- hsv [:, :, 1 ] = np .where (np .logical_and (
1383
- np .abs (hsv [:, :, 1 ]) > 1.e-10 ,
1384
- intensity < 0 ),
1385
- (1. + intensity ) * hsv [:, :, 1 ] -
1386
- intensity * self .hsv_min_sat ,
1387
- hsv [:, :, 1 ])
1377
+ hsv [:, :, 1 ] = np .where (np .logical_and (np .abs (hsv [:, :, 1 ]) > 1.e-10 ,
1378
+ intensity < 0 ),
1379
+ ((1. + intensity ) * hsv [:, :, 1 ] -
1380
+ intensity * self .hsv_min_sat ),
1381
+ hsv [:, :, 1 ])
1388
1382
hsv [:, :, 2 ] = np .where (intensity < 0 ,
1389
- (1. + intensity ) * hsv [:, :, 2 ] -
1390
- intensity * self .hsv_min_val ,
1391
- hsv [:, :, 2 ])
1383
+ ( (1. + intensity ) * hsv [:, :, 2 ] -
1384
+ intensity * self .hsv_min_val ) ,
1385
+ hsv [:, :, 2 ])
1392
1386
hsv [:, :, 1 :] = np .where (hsv [:, :, 1 :] < 0. , 0 , hsv [:, :, 1 :])
1393
1387
hsv [:, :, 1 :] = np .where (hsv [:, :, 1 :] > 1. , 1 , hsv [:, :, 1 :])
1394
1388
# convert modified hsv back to rgb.
0 commit comments