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

Skip to content

Commit f5c6ad7

Browse files
committed
Removed deprecated float-as-gray
svn path=/trunk/matplotlib/; revision=3163
1 parent 2cc0a65 commit f5c6ad7

4 files changed

Lines changed: 82 additions & 95 deletions

File tree

API_CHANGES

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
Removed deprecated support for a float value as a gray-scale;
2+
now it must be a string, like '0.5'. Added alpha kwarg to
3+
ColorConverter.to_rgba_list.
4+
15
New method set_bounds(vmin, vmax) for formatters, locators sets
26
the viewInterval and dataInterval from floats.
37

CHANGELOG

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
2007-04-06 Fixed rotation of ellipses in pdf backend
1+
2007-04-06 Removed deprecated support for a float value as a gray-scale;
2+
now it must be a string, like '0.5'. Added alpha kwarg to
3+
ColorConverter.to_rgba_list. - EF
4+
5+
2007-04-06 Fixed rotation of ellipses in pdf backend
26
(sf bug #1690559) -JKS
37

48
2007-04-04 More matshow tweaks; documentation updates; new method

lib/matplotlib/axes.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from collections import RegularPolyCollection, PolyCollection, LineCollection, \
2121
QuadMesh, StarPolygonCollection, BrokenBarHCollection
2222
from colors import colorConverter, Normalize, Colormap, \
23-
LinearSegmentedColormap, ListedColormap, looks_like_color, is_color_like
23+
LinearSegmentedColormap, ListedColormap, is_color_like
2424
import cm
2525
from cm import ScalarMappable
2626
from contour import ContourSet
@@ -3696,7 +3696,7 @@ def scatter(self, x, y, s=20, c='b', marker='o', cmap=None, norm=None,
36963696
36973697
Make a scatter plot of x versus y. s is a size in points^2 a scalar
36983698
or an array of the same length as x or y. c is a color and can be a
3699-
single color format string or an length(x) array of intensities which
3699+
single color format string or a length(x) array of intensities which
37003700
will be mapped by the matplotlib.colors.colormap instance cmap
37013701
37023702
The marker can be one of
@@ -3770,7 +3770,8 @@ def scatter(self, x, y, s=20, c='b', marker='o', cmap=None, norm=None,
37703770

37713771
x, y, s, c = delete_masked_points(x, y, s, c)
37723772

3773-
3773+
# Strange kwarg override: kwargs['color'] overrides
3774+
# defaulted kw 'c':
37743775
if kwargs.has_key('color'):
37753776
c = kwargs['color']
37763777
kwargs.pop('color')

lib/matplotlib/colors.py

Lines changed: 69 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
A class for converting color arguments to RGB
2+
A class for converting color arguments to RGB or RGBA
33
44
This class instantiates a single instance colorConverter that is used
55
to convert matlab color strings to RGB. RGB is a tuple of float RGB
@@ -17,6 +17,11 @@
1717
k : black
1818
w : white
1919
20+
Gray shades can be given as a string encoding a float in the 0-1
21+
range, e.g.,
22+
23+
color = '0.75'
24+
2025
For a greater range of colors, you have two options. You can specify
2126
the color using an html hex string, as in
2227
@@ -189,22 +194,6 @@
189194
k = k.replace('gray', 'grey')
190195
cnames[k] = v
191196

192-
def looks_like_color(c):
193-
warnings.warn('Use is_color_like instead!', DeprecationWarning)
194-
if is_string_like(c):
195-
if cnames.has_key(c): return True
196-
elif len(c)==1: return True
197-
elif len(c)==7 and c.startswith('#') and len(c)==7: return True
198-
else: return False
199-
elif iterable(c) and len(c)==3:
200-
try:
201-
rgb = [float(val) for val in c]
202-
return True
203-
except:
204-
return False
205-
else:
206-
return False
207-
208197
def is_color_like(c):
209198
try:
210199
colorConverter.to_rgb(c)
@@ -243,117 +232,106 @@ class ColorConverter:
243232
}
244233

245234
cache = {}
246-
def to_rgb(self, arg, warn=True):
235+
def to_rgb(self, arg):
247236
"""
248237
Returns an RGB tuple of three floats from 0-1.
249238
250-
arg can be an RGB sequence or a string in any of several forms:
239+
arg can be an RGB or RGBA sequence or a string in any of several forms:
251240
1) a letter from the set 'rgbcmykw'
252241
2) a hex color string, like '#00FFFF'
253242
3) a standard name, like 'aqua'
254243
4) a float, like '0.4', indicating gray on a 0-1 scale
244+
245+
if arg is RGBA, the A will simply be discarded.
255246
"""
256-
# warn kwarg will go away when float-as-grayscale does
257247
try: return self.cache[arg]
258248
except KeyError: pass
259249
except TypeError: # could be unhashable rgb seq
260250
arg = tuple(arg)
261-
try: self.cache[arg]
251+
try: return self.cache[arg]
262252
except KeyError: pass
263253
except TypeError:
264-
raise ValueError('to_rgb: unhashable even inside a tuple')
254+
raise ValueError(
255+
'to_rgb: arg "%s" is unhashable even inside a tuple'
256+
% (str(arg),))
265257

266258
try:
267259
if is_string_like(arg):
268-
str1 = cnames.get(arg, arg)
269-
if str1.startswith('#'):
270-
color = hex2color(str1)
271-
else:
272-
try:
273-
color = self.colors[arg]
274-
except KeyError:
275-
color = tuple([float(arg)]*3)
276-
elif iterable(arg): # streamline this after removing float case
260+
color = self.colors.get(arg, None)
261+
if color is None:
262+
str1 = cnames.get(arg, arg)
263+
if str1.startswith('#'):
264+
color = hex2color(str1)
265+
else:
266+
fl = float(arg)
267+
if fl < 0 or fl > 1:
268+
raise ValueError(
269+
'gray (string) must be in range 0-1')
270+
color = tuple([fl]*3)
271+
elif iterable(arg):
272+
if len(arg) > 4 or len(arg) < 3:
273+
raise ValueError(
274+
'sequence length is %d; must be 3 or 4'%len(arg))
277275
color = tuple(arg[:3])
278-
if [x for x in color if (x < 0) or (x > 1)]:
279-
raise ValueError('to_rgb: Invalid rgb arg "%s"' % (str(arg)))
280-
elif isinstance(arg, (float,int)):
281-
#raise Exception('number is %s' % str(arg))
282-
if warn: warnings.warn(
283-
"For gray use a string, '%s', not a float, %s" %
284-
(str(arg), str(arg)),
285-
DeprecationWarning)
286-
else: self._gray = True
287-
if 0 <= arg <= 1:
288-
color = (arg,arg,arg)
289-
else:
290-
raise ValueError('Floating point color arg must be between 0 and 1')
276+
if [x for x in color if (float(x) < 0) or (x > 1)]:
277+
# This will raise TypeError if x is not a number.
278+
raise ValueError('number in rbg sequence outside 0-1 range')
291279
else:
292-
raise ValueError('to_rgb: Invalid rgb arg "%s"' % (str(arg)))
280+
raise ValueError('cannot convert argument to rgb sequence')
293281

294282
self.cache[arg] = color
295283

296284
except (KeyError, ValueError, TypeError), exc:
297285
raise ValueError('to_rgb: Invalid rgb arg "%s"\n%s' % (str(arg), exc))
298-
286+
# Error messages could be improved by handling TypeError
287+
# separately; but this should be rare and not too hard
288+
# for the user to figure out as-is.
299289
return color
300290

301-
def to_rgba(self, arg, alpha=None, warn=True):
291+
def to_rgba(self, arg, alpha=None):
302292
"""
303293
Returns an RGBA tuple of four floats from 0-1.
304294
305-
For acceptable values of arg, see to_rgb. In
306-
addition, arg may already be an rgba sequence, in which
307-
case it is returned unchanged if the alpha kwarg is None,
308-
or takes on the specified alpha.
295+
For acceptable values of arg, see to_rgb.
296+
If arg is an RGBA sequence and alpha is not None,
297+
alpha will replace the original A.
309298
"""
310-
if not is_string_like(arg) and iterable(arg):
311-
if len(arg) == 4 and alpha is None:
312-
return tuple(arg)
313-
r,g,b = arg[:3]
314-
else:
315-
r,g,b = self.to_rgb(arg, warn)
316-
if alpha is None:
317-
alpha = 1.0
318-
return r,g,b,alpha
319-
320-
def to_rgba_list(self, c):
299+
try:
300+
if not is_string_like(arg) and iterable(arg):
301+
if len(arg) == 4 and alpha is None:
302+
if [x for x in arg if (float(x) < 0) or (x > 1)]:
303+
# This will raise TypeError if x is not a number.
304+
raise ValueError('number in rbga sequence outside 0-1 range')
305+
return tuple(arg)
306+
r,g,b = arg[:3]
307+
if [x for x in (r,g,b) if (float(x) < 0) or (x > 1)]:
308+
raise ValueError('number in rbg sequence outside 0-1 range')
309+
else:
310+
r,g,b = self.to_rgb(arg)
311+
if alpha is None:
312+
alpha = 1.0
313+
return r,g,b,alpha
314+
except (TypeError, ValueError), exc:
315+
raise ValueError('to_rgba: Invalid rgba arg "%s"\n%s' % (str(arg), exc))
316+
317+
def to_rgba_list(self, c, alpha=None):
321318
"""
322319
Returns a list of rgba tuples.
323320
324321
Accepts a single mpl color spec or a sequence of specs.
325322
If the sequence is a list, the list items are changed in place.
326323
"""
327-
# This can be improved after removing float-as-grayscale.
328-
if not is_string_like(c):
329-
try:
330-
N = len(c) # raises TypeError if it is not a sequence
331-
# Temporary hack: keep single rgb or rgba from being
332-
# treated as grayscale.
333-
if N==3 or N==4:
334-
L = [x for x in c if x>=0 and x<=1]
335-
if len(L) == N:
336-
raise ValueError
337-
# If c is a list, we need to return the same list but
338-
# with modified items so that items can be appended to
339-
# it. This is needed for examples/dynamic_collections.py.
340-
if not isinstance(c, list): # specific; don't need duck-typing
341-
c = list(c)
342-
self._gray = False
343-
for i, cc in enumerate(c):
344-
c[i] = self.to_rgba(cc, warn=False) # change in place
345-
if self._gray:
346-
msg = "In argument %s: use string, not float, for grayscale" % str(c)
347-
warnings.warn(msg, DeprecationWarning)
348-
return c
349-
except (ValueError, TypeError):
350-
pass
351324
try:
352-
return [self.to_rgba(c)]
353-
except (ValueError, TypeError):
354-
raise TypeError('c must be a matplotlib color arg or a sequence of them')
355-
356-
325+
return [self.to_rgba(c, alpha)]
326+
except ValueError:
327+
# If c is a list it must be maintained as the same list
328+
# with modified items so that items can be appended to
329+
# it. This is needed for examples/dynamic_collections.py.
330+
if not isinstance(c, list): # specific; don't need duck-typing
331+
c = list(c)
332+
for i, cc in enumerate(c):
333+
c[i] = self.to_rgba(cc, alpha) # change in place
334+
return c
357335

358336
colorConverter = ColorConverter()
359337

0 commit comments

Comments
 (0)