Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1b346e0

Browse filesBrowse files
committed
Merge pull request #1797 from NelleV/pep8_colors
PEP8 on colors module
2 parents 56df912 + 2581bfb commit 1b346e0
Copy full SHA for 1b346e0

File tree

Expand file treeCollapse file tree

1 file changed

+65
-71
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+65
-71
lines changed

‎lib/matplotlib/colors.py

Copy file name to clipboardExpand all lines: lib/matplotlib/colors.py
+65-71Lines changed: 65 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
list of color specifications.
1717
1818
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*.
2121
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
2424
2525
- b: blue
2626
- g: green
@@ -31,21 +31,20 @@
3131
- k: black
3232
- w: white
3333
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.::
3635
3736
color = '0.75'
3837
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::
4140
4241
color = '#eeefff'
4342
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].
4645
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.
4948
"""
5049
from __future__ import print_function, division
5150
import re
@@ -198,8 +197,7 @@
198197
'white': '#FFFFFF',
199198
'whitesmoke': '#F5F5F5',
200199
'yellow': '#FFFF00',
201-
'yellowgreen': '#9ACD32',
202-
}
200+
'yellowgreen': '#9ACD32'}
203201

204202

205203
# add british equivs
@@ -255,8 +253,7 @@ class ColorConverter(object):
255253
'm': (0.75, 0, 0.75),
256254
'y': (0.75, 0.75, 0),
257255
'k': (0.0, 0.0, 0.0),
258-
'w': (1.0, 1.0, 1.0),
259-
}
256+
'w': (1.0, 1.0, 1.0), }
260257

261258
cache = {}
262259

@@ -286,8 +283,8 @@ def to_rgb(self, arg):
286283
pass
287284
except TypeError:
288285
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),))
291288

292289
try:
293290
if cbook.is_string_like(arg):
@@ -301,26 +298,26 @@ def to_rgb(self, arg):
301298
fl = float(argl)
302299
if fl < 0 or fl > 1:
303300
raise ValueError(
304-
'gray (string) must be in range 0-1')
301+
'gray (string) must be in range 0-1')
305302
color = tuple([fl] * 3)
306303
elif cbook.iterable(arg):
307304
if len(arg) > 4 or len(arg) < 3:
308305
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))
310307
color = tuple(arg[:3])
311308
if [x for x in color if (float(x) < 0) or (x > 1)]:
312309
# This will raise TypeError if x is not a number.
313310
raise ValueError(
314-
'number in rbg sequence outside 0-1 range')
311+
'number in rbg sequence outside 0-1 range')
315312
else:
316313
raise ValueError(
317-
'cannot convert argument to rgb sequence')
314+
'cannot convert argument to rgb sequence')
318315

319316
self.cache[arg] = color
320317

321318
except (KeyError, ValueError, TypeError) as exc:
322319
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))
324321
# Error messages could be improved by handling TypeError
325322
# separately; but this should be rare and not too hard
326323
# for the user to figure out as-is.
@@ -348,7 +345,7 @@ def to_rgba(self, arg, alpha=None):
348345
if [x for x in arg if (float(x) < 0) or (x > 1)]:
349346
# This will raise TypeError if x is not a number.
350347
raise ValueError(
351-
'number in rbga sequence outside 0-1 range')
348+
'number in rbga sequence outside 0-1 range')
352349
if alpha is None:
353350
return tuple(arg)
354351
if alpha < 0.0 or alpha > 1.0:
@@ -357,15 +354,15 @@ def to_rgba(self, arg, alpha=None):
357354
r, g, b = arg[:3]
358355
if [x for x in (r, g, b) if (float(x) < 0) or (x > 1)]:
359356
raise ValueError(
360-
'number in rbg sequence outside 0-1 range')
357+
'number in rbg sequence outside 0-1 range')
361358
else:
362359
r, g, b = self.to_rgb(arg)
363360
if alpha is None:
364361
alpha = 1.0
365362
return r, g, b, alpha
366363
except (TypeError, ValueError) as exc:
367364
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))
369366

370367
def to_rgba_array(self, c, alpha=None):
371368
"""
@@ -458,18 +455,18 @@ def makeMappingArray(N, data, gamma=1.0):
458455

459456
if x[0] != 0. or x[-1] != 1.0:
460457
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")
462459
if np.sometrue(np.sort(x) - x):
463460
raise ValueError(
464-
"data mapping points must have x in increasing order")
461+
"data mapping points must have x in increasing order")
465462
# begin generation of lookup table
466463
x = x * (N - 1)
467464
lut = np.zeros((N,), np.float)
468465
xind = (N - 1) * np.linspace(0, 1, N) ** gamma
469466
ind = np.searchsorted(x, xind)[1:-1]
470467

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])
473470
lut[0] = y1[0]
474471
lut[-1] = y0[-1]
475472
# ensure that the lut is confined to values between 0 and 1 by clipping it
@@ -635,8 +632,8 @@ def _init(self):
635632
def is_gray(self):
636633
if not self._isinit:
637634
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]))
640637

641638

642639
class LinearSegmentedColormap(Colormap):
@@ -701,15 +698,15 @@ def __init__(self, name, segmentdata, N=256, gamma=1.0):
701698

702699
def _init(self):
703700
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)
710707
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)
713710
self._isinit = True
714711
self._set_extremes()
715712

@@ -955,7 +952,7 @@ def __call__(self, value, clip=None):
955952
if clip:
956953
mask = ma.getmask(result)
957954
result = ma.array(np.clip(result.filled(vmax), vmin, vmax),
958-
mask=mask)
955+
mask=mask)
959956
# in-place equivalent of above can be much faster
960957
resdat = result.data
961958
mask = result.mask
@@ -1000,7 +997,7 @@ def autoscale_None(self, A):
1000997
self.vmin = ma.min(A)
1001998
if self.vmax is None:
1002999
self.vmax = ma.max(A)
1003-
1000+
10041001

10051002
class SymLogNorm(Normalize):
10061003
"""
@@ -1039,7 +1036,7 @@ def __call__(self, value, clip=None):
10391036
result, is_scalar = self.process_value(value)
10401037
self.autoscale_None(result)
10411038
vmin, vmax = self.vmin, self.vmax
1042-
1039+
10431040
if vmin > vmax:
10441041
raise ValueError("minvalue must be less than or equal to maxvalue")
10451042
elif vmin == vmax:
@@ -1048,7 +1045,7 @@ def __call__(self, value, clip=None):
10481045
if clip:
10491046
mask = ma.getmask(result)
10501047
result = ma.array(np.clip(result.filled(vmax), vmin, vmax),
1051-
mask=mask)
1048+
mask=mask)
10521049
# in-place equivalent of above can be much faster
10531050
resdat = self._transform(result.data)
10541051
resdat -= self._lower
@@ -1057,8 +1054,8 @@ def __call__(self, value, clip=None):
10571054
if is_scalar:
10581055
result = result[0]
10591056
return result
1060-
1061-
def _transform(self, a):
1057+
1058+
def _transform(self, a):
10621059
"""
10631060
Inplace transformation.
10641061
"""
@@ -1069,7 +1066,7 @@ def _transform(self, a):
10691066
a[masked] = log
10701067
a[~masked] *= self._linscale_adj
10711068
return a
1072-
1069+
10731070
def _inv_transform(self, a):
10741071
"""
10751072
Inverse inplace Transformation.
@@ -1081,31 +1078,30 @@ def _inv_transform(self, a):
10811078
a[masked] = exp
10821079
a[~masked] /= self._linscale_adj
10831080
return a
1084-
1081+
10851082
def _transform_vmin_vmax(self):
10861083
"""
10871084
Calculates vmin and vmax in the transformed system.
10881085
"""
10891086
vmin, vmax = self.vmin, self.vmax
10901087
arr = np.array([vmax, vmin])
1091-
self._upper, self._lower = self._transform(arr)
1092-
1088+
self._upper, self._lower = self._transform(arr)
10931089

10941090
def inverse(self, value):
10951091
if not self.scaled():
10961092
raise ValueError("Not invertible until scaled")
10971093
val = ma.asarray(value)
10981094
val = val * (self._upper - self._lower) + self._lower
10991095
return self._inv_transform(val)
1100-
1096+
11011097
def autoscale(self, A):
11021098
"""
11031099
Set *vmin*, *vmax* to min, max of *A*.
1104-
"""
1100+
"""
11051101
self.vmin = ma.min(A)
11061102
self.vmax = ma.max(A)
11071103
self._transform_vmin_vmax()
1108-
1104+
11091105
def autoscale_None(self, A):
11101106
""" autoscale only None-valued vmin or vmax """
11111107
if self.vmin is not None and self.vmax is not None:
@@ -1355,8 +1351,8 @@ def shade_rgb(self, rgb, elevation, fraction=1.):
13551351
dx, dy = np.gradient(elevation)
13561352
slope = 0.5 * np.pi - np.arctan(np.hypot(dx, dy))
13571353
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))
13601356
# rescale to interval -1,1
13611357
# +1 means maximum sun exposure and -1 means complete shade.
13621358
intensity = (intensity - intensity.min()) / \
@@ -1367,28 +1363,26 @@ def shade_rgb(self, rgb, elevation, fraction=1.):
13671363
hsv = rgb_to_hsv(rgb[:, :, 0:3])
13681364
# modify hsv values to simulate illumination.
13691365

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),
13751370
hsv[:, :, 1])
13761371

13771372
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),
13801375
hsv[:, :, 2])
13811376

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])
13881382
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])
13921386
hsv[:, :, 1:] = np.where(hsv[:, :, 1:] < 0., 0, hsv[:, :, 1:])
13931387
hsv[:, :, 1:] = np.where(hsv[:, :, 1:] > 1., 1, hsv[:, :, 1:])
13941388
# convert modified hsv back to rgb.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.