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

Skip to content

Commit fee28d0

Browse files
committed
Remove use of colorConverter.
Except in Qt options editor, which is under a separate PR. This also makes it possible to call plt.plot([1, 2, 3], "#ff000040") (setting alpha at the same time).
1 parent 8ff6673 commit fee28d0

26 files changed

+106
-164
lines changed

examples/api/collections_demo.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
'''
1818

1919
import matplotlib.pyplot as plt
20-
from matplotlib import collections, transforms
21-
from matplotlib.colors import colorConverter
20+
from matplotlib import collections, colors, transforms
2221
import numpy as np
2322

2423
nverts = 50
@@ -38,7 +37,7 @@
3837
xyo = list(zip(xo, yo))
3938

4039
# Make a list of colors cycling through the default series.
41-
colors = [colorConverter.to_rgba(c)
40+
colors = [colors.to_rgba(c)
4241
for c in plt.rcParams['axes.prop_cycle'].by_key()['color']]
4342

4443
fig, axes = plt.subplots(2, 2)

examples/event_handling/lasso_demo.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@
77
usable as is). There will be some refinement of the API.
88
"""
99
from matplotlib.widgets import Lasso
10-
from matplotlib.colors import colorConverter
1110
from matplotlib.collections import RegularPolyCollection
12-
from matplotlib import path
11+
from matplotlib import colors as mcolors, path
1312

1413
import matplotlib.pyplot as plt
1514
from numpy import nonzero
1615
from numpy.random import rand
1716

1817

1918
class Datum(object):
20-
colorin = colorConverter.to_rgba('red')
21-
colorout = colorConverter.to_rgba('blue')
19+
colorin = mcolors.to_rgba("red")
20+
colorout = mcolors.to_rgba("blue")
2221

2322
def __init__(self, x, y, include=False):
2423
self.x = x

examples/mplot3d/polys3d_demo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55

66
from mpl_toolkits.mplot3d import Axes3D
77
from matplotlib.collections import PolyCollection
8-
from matplotlib.colors import colorConverter
98
import matplotlib.pyplot as plt
9+
from matplotlib import colors as mcolors
1010
import numpy as np
1111

1212

1313
def cc(arg):
1414
'''
1515
Shorthand to convert 'named' colors to rgba format at 60% opacity.
1616
'''
17-
return colorConverter.to_rgba(arg, alpha=0.6)
17+
return mcolors.to_rgba(arg, alpha=0.6)
1818

1919

2020
def polygon_under_graph(xlist, ylist):

examples/pylab_examples/colours.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
Some simple functions to generate colours.
44
"""
55
import numpy as np
6-
from matplotlib.colors import colorConverter
6+
from matplotlib import colors as mcolors
77

88

99
def pastel(colour, weight=2.4):
1010
""" Convert colour into a nice pastel shade"""
11-
rgb = np.asarray(colorConverter.to_rgb(colour))
11+
rgb = np.asarray(mcolors.to_rgba(colour)[:3])
1212
# scale colour
1313
maxc = max(rgb)
1414
if maxc < 1.0 and maxc > 0:

examples/pylab_examples/demo_ribbon_box.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class RibbonBox(object):
1818
nx = original_image.shape[1]
1919

2020
def __init__(self, color):
21-
rgb = matplotlib.colors.colorConverter.to_rgb(color)
21+
rgb = matplotlib.colors.to_rgba(color)[:3]
2222

2323
im = np.empty(self.original_image.shape,
2424
self.original_image.dtype)

examples/pylab_examples/line_collection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import matplotlib.pyplot as plt
22
from matplotlib.collections import LineCollection
3-
from matplotlib.colors import colorConverter
3+
from matplotlib import colors as mcolors
44

55
import numpy as np
66

@@ -30,7 +30,7 @@
3030
# where onoffseq is an even length tuple of on and off ink in points.
3131
# If linestyle is omitted, 'solid' is used
3232
# See matplotlib.collections.LineCollection for more information
33-
colors = [colorConverter.to_rgba(c)
33+
colors = [mcolors.to_rgba(c)
3434
for c in plt.rcParams['axes.prop_cycle'].by_key()['color']]
3535

3636
line_segments = LineCollection(segs, linewidths=(0.5, 1, 1.5, 2),

examples/widgets/menu.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ def __init__(self, fontsize=14, labelcolor='black', bgcolor='yellow',
1717
self.bgcolor = bgcolor
1818
self.alpha = alpha
1919

20-
self.labelcolor_rgb = colors.colorConverter.to_rgb(labelcolor)
21-
self.bgcolor_rgb = colors.colorConverter.to_rgb(bgcolor)
20+
self.labelcolor_rgb = colors.to_rgba(labelcolor)[:3]
21+
self.bgcolor_rgb = colors.to_rgba(bgcolor)[:3]
2222

2323

2424
class MenuItem(artist.Artist):

lib/matplotlib/axes/_axes.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,7 +2065,7 @@ def make_iterable(x):
20652065
if color is None:
20662066
color = [None] * nbars
20672067
else:
2068-
color = list(mcolors.colorConverter.to_rgba_array(color))
2068+
color = list(mcolors.to_rgba_array(color))
20692069
if len(color) == 0: # until to_rgba_array is changed
20702070
color = [[0, 0, 0, 0]]
20712071
if len(color) < nbars:
@@ -2074,7 +2074,7 @@ def make_iterable(x):
20742074
if edgecolor is None:
20752075
edgecolor = [None] * nbars
20762076
else:
2077-
edgecolor = list(mcolors.colorConverter.to_rgba_array(edgecolor))
2077+
edgecolor = list(mcolors.to_rgba_array(edgecolor))
20782078
if len(edgecolor) == 0: # until to_rgba_array is changed
20792079
edgecolor = [[0, 0, 0, 0]]
20802080
if len(edgecolor) < nbars:
@@ -3846,7 +3846,7 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
38463846
co = kwargs.pop('color', None)
38473847
if co is not None:
38483848
try:
3849-
mcolors.colorConverter.to_rgba_array(co)
3849+
mcolors.to_rgba_array(co)
38503850
except ValueError:
38513851
raise ValueError("'color' kwarg must be an mpl color"
38523852
" spec or sequence of color specs.\n"
@@ -6045,7 +6045,7 @@ def _normalize_input(inp, ename='input'):
60456045
if color is None:
60466046
color = [self._get_lines.get_next_color() for i in xrange(nx)]
60476047
else:
6048-
color = mcolors.colorConverter.to_rgba_array(color)
6048+
color = mcolors.to_rgba_array(color)
60496049
if len(color) != nx:
60506050
raise ValueError("color kwarg must have one color per dataset")
60516051

lib/matplotlib/axes/_base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def _process_plot_format(fmt):
6868

6969
# Is fmt just a colorspec?
7070
try:
71-
color = mcolors.colorConverter.to_rgb(fmt)
71+
color = mcolors.to_rgba(fmt)
7272

7373
# We need to differentiate grayscale '1.0' from tri_down marker '1'
7474
try:
@@ -112,14 +112,14 @@ def _process_plot_format(fmt):
112112
raise ValueError(
113113
'Illegal format string "%s"; two marker symbols' % fmt)
114114
marker = c
115-
elif c in mcolors.colorConverter.colors:
115+
elif c in mcolors.get_named_colors_mapping():
116116
if color is not None:
117117
raise ValueError(
118118
'Illegal format string "%s"; two color symbols' % fmt)
119119
color = c
120120
elif c == 'C' and i < len(chars) - 1:
121121
color_cycle_number = int(chars[i + 1])
122-
color = mcolors.colorConverter._get_nth_color(color_cycle_number)
122+
color = mcolors.to_rgba("C{}".format(color_cycle_number))
123123
i += 1
124124
else:
125125
raise ValueError(
@@ -3687,7 +3687,7 @@ def set_cursor_props(self, *args):
36873687
lw, c = args
36883688
else:
36893689
raise ValueError('args must be a (linewidth, color) tuple')
3690-
c = mcolors.colorConverter.to_rgba(c)
3690+
c = mcolors.to_rgba(c)
36913691
self._cursorProps = lw, c
36923692

36933693
def get_children(self):

lib/matplotlib/backend_bases.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,11 +1022,11 @@ def set_foreground(self, fg, isRGBA=False):
10221022
if self._forced_alpha and isRGBA:
10231023
self._rgb = fg[:3] + (self._alpha,)
10241024
elif self._forced_alpha:
1025-
self._rgb = colors.colorConverter.to_rgba(fg, self._alpha)
1025+
self._rgb = colors.to_rgba(fg, self._alpha)
10261026
elif isRGBA:
10271027
self._rgb = fg
10281028
else:
1029-
self._rgb = colors.colorConverter.to_rgba(fg)
1029+
self._rgb = colors.to_rgba(fg)
10301030

10311031
def set_graylevel(self, frac):
10321032
"""

lib/matplotlib/backends/backend_agg.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -568,9 +568,8 @@ def print_jpg(self, filename_or_obj, *args, **kwargs):
568568
# The image is "pasted" onto a white background image to safely
569569
# handle any transparency
570570
image = Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1)
571-
color = mcolors.colorConverter.to_rgb(
572-
rcParams.get('savefig.facecolor', 'white'))
573-
color = tuple([int(x * 255.0) for x in color])
571+
rgba = mcolors.to_rgba(rcParams.get('savefig.facecolor', 'white'))
572+
color = tuple([int(x * 255.0) for x in rgba[:3]])
574573
background = Image.new('RGB', size, color)
575574
background.paste(image, image)
576575
options = restrict_dict(kwargs, ['quality', 'optimize',

lib/matplotlib/backends/backend_gtk.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,11 @@ def fn_name(): return sys._getframe(1).f_code.co_name
3535

3636
from matplotlib.backends.backend_gdk import RendererGDK, FigureCanvasGDK
3737
from matplotlib.cbook import is_string_like, is_writable_file_like
38-
from matplotlib.colors import colorConverter
3938
from matplotlib.figure import Figure
4039
from matplotlib.widgets import SubplotTool
4140

42-
from matplotlib import lines
43-
from matplotlib import markers
44-
from matplotlib import cbook
45-
from matplotlib import verbose
46-
from matplotlib import rcParams
41+
from matplotlib import (
42+
cbook, colors as mcolors, lines, markers, rcParams, verbose)
4743

4844
backend_version = "%d.%d.%d" % gtk.pygtk_version
4945

@@ -1003,13 +999,13 @@ def on_combobox_lineprops_changed(self, item):
1003999
if marker is None: marker = 'None'
10041000
self.cbox_markers.set_active(self.markerd[marker])
10051001

1006-
r,g,b = colorConverter.to_rgb(line.get_color())
1007-
color = gtk.gdk.Color(*[int(val*65535) for val in (r,g,b)])
1002+
rgba = mcolors.to_rgba(line.get_color())
1003+
color = gtk.gdk.Color(*[int(val*65535) for val in rgba[:3]])
10081004
button = self.wtree.get_widget('colorbutton_linestyle')
10091005
button.set_color(color)
10101006

1011-
r,g,b = colorConverter.to_rgb(line.get_markerfacecolor())
1012-
color = gtk.gdk.Color(*[int(val*65535) for val in (r,g,b)])
1007+
rgba = mcolors.to_rgba(line.get_markerfacecolor())
1008+
color = gtk.gdk.Color(*[int(val*65535) for val in rgba[:3]])
10131009
button = self.wtree.get_widget('colorbutton_markerface')
10141010
button.set_color(color)
10151011
self._updateson = True

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,10 @@ def fn_name(): return sys._getframe(1).f_code.co_name
3636
from matplotlib import backend_tools
3737

3838
from matplotlib.cbook import is_string_like, is_writable_file_like
39-
from matplotlib.colors import colorConverter
4039
from matplotlib.figure import Figure
4140
from matplotlib.widgets import SubplotTool
4241

43-
from matplotlib import lines
44-
from matplotlib import cbook
45-
from matplotlib import verbose
46-
from matplotlib import rcParams
42+
from matplotlib import cbook, colors as mcolors, lines, verbose, rcParams
4743

4844
backend_version = "%s.%s.%s" % (Gtk.get_major_version(), Gtk.get_micro_version(), Gtk.get_minor_version())
4945

@@ -1063,13 +1059,13 @@ def on_combobox_lineprops_changed(self, item):
10631059
if marker is None: marker = 'None'
10641060
self.cbox_markers.set_active(self.markerd[marker])
10651061

1066-
r,g,b = colorConverter.to_rgb(line.get_color())
1067-
color = Gdk.Color(*[int(val*65535) for val in (r,g,b)])
1062+
rgba = mcolors.to_rgba(line.get_color())
1063+
color = Gdk.Color(*[int(val*65535) for val in rgba[:3]])
10681064
button = self.wtree.get_widget('colorbutton_linestyle')
10691065
button.set_color(color)
10701066

1071-
r,g,b = colorConverter.to_rgb(line.get_markerfacecolor())
1072-
color = Gdk.Color(*[int(val*65535) for val in (r,g,b)])
1067+
rgba = mcolors.to_rgba(line.get_markerfacecolor())
1068+
color = Gdk.Color(*[int(val*65535) for val in rgba[:3]])
10731069
button = self.wtree.get_widget('colorbutton_markerface')
10741070
button.set_color(color)
10751071
self._updateson = True

lib/matplotlib/collections.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ def set_facecolor(self, c):
615615
if c is None:
616616
c = mpl.rcParams['patch.facecolor']
617617
self._facecolors_original = c
618-
self._facecolors = mcolors.colorConverter.to_rgba_array(c, self._alpha)
618+
self._facecolors = mcolors.to_rgba_array(c, self._alpha)
619619
self.stale = True
620620

621621
def set_facecolors(self, c):
@@ -663,7 +663,7 @@ def set_edgecolor(self, c):
663663
if c is None:
664664
c = mpl.rcParams['patch.edgecolor']
665665
self._edgecolors_original = c
666-
self._edgecolors = mcolors.colorConverter.to_rgba_array(c, self._alpha)
666+
self._edgecolors = mcolors.to_rgba_array(c, self._alpha)
667667
self.stale = True
668668

669669
def set_edgecolors(self, c):
@@ -684,14 +684,14 @@ def set_alpha(self, alpha):
684684
raise TypeError('alpha must be a float or None')
685685
artist.Artist.set_alpha(self, alpha)
686686
try:
687-
self._facecolors = mcolors.colorConverter.to_rgba_array(
687+
self._facecolors = mcolors.to_rgba_array(
688688
self._facecolors_original, self._alpha)
689689
except (AttributeError, TypeError, IndexError):
690690
pass
691691
try:
692692
if (not isinstance(self._edgecolors_original, six.string_types)
693693
or self._edgecolors_original != str('face')):
694-
self._edgecolors = mcolors.colorConverter.to_rgba_array(
694+
self._edgecolors = mcolors.to_rgba_array(
695695
self._edgecolors_original, self._alpha)
696696
except (AttributeError, TypeError, IndexError):
697697
pass
@@ -1137,7 +1137,7 @@ def __init__(self, segments, # Can be None.
11371137
if antialiaseds is None:
11381138
antialiaseds = (mpl.rcParams['lines.antialiased'],)
11391139

1140-
colors = mcolors.colorConverter.to_rgba_array(colors)
1140+
colors = mcolors.to_rgba_array(colors)
11411141

11421142
Collection.__init__(
11431143
self,

0 commit comments

Comments
 (0)