Skip to content

Commit 5e77085

Browse files
committed
ENH/API: pass cycler into to_rgba to resolve CN
Be able to over-ride the global property cycler for CN lookup by passing in a kwarg.
1 parent dfa14fe commit 5e77085

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

lib/matplotlib/axes/_base.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
is_sequence_of_strings = cbook.is_sequence_of_strings
4545

4646

47-
def _process_plot_format(fmt):
47+
def _process_plot_format(fmt, prop_cycler=None):
4848
"""
4949
Process a MATLAB style color/line style format string. Return a
5050
(*linestyle*, *color*) tuple as a result of the processing. Default
@@ -68,7 +68,7 @@ def _process_plot_format(fmt):
6868

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

7373
# We need to differentiate grayscale '1.0' from tri_down marker '1'
7474
try:
@@ -119,7 +119,8 @@ def _process_plot_format(fmt):
119119
color = c
120120
elif c == 'C' and i < len(chars) - 1:
121121
color_cycle_number = int(chars[i + 1])
122-
color = mcolors.to_rgba("C{}".format(color_cycle_number))
122+
color = mcolors.to_rgba("C{}".format(color_cycle_number),
123+
prop_cycler=prop_cycler)
123124
i += 1
124125
else:
125126
raise ValueError(
@@ -168,6 +169,8 @@ def set_prop_cycle(self, *args, **kwargs):
168169
prop_cycler = cycler(*args, **kwargs)
169170

170171
self.prop_cycler = itertools.cycle(prop_cycler)
172+
# keep a private copy of it
173+
self._prop_cycler_obj = prop_cycler
171174
# This should make a copy
172175
self._prop_keys = prop_cycler.keys
173176

@@ -350,7 +353,8 @@ def _makefill(self, x, y, kw, kwargs):
350353
def _plot_args(self, tup, kwargs):
351354
ret = []
352355
if len(tup) > 1 and is_string_like(tup[-1]):
353-
linestyle, marker, color = _process_plot_format(tup[-1])
356+
linestyle, marker, color = _process_plot_format(
357+
tup[-1], self._prop_cycler_obj)
354358
tup = tup[:-1]
355359
elif len(tup) == 3:
356360
raise ValueError('third arg must be a format string')

lib/matplotlib/colors.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,16 @@ def is_color_like(c):
118118
return True
119119

120120

121-
def to_rgba(c, alpha=None):
121+
def to_rgba(c, alpha=None, prop_cycler=None):
122122
"""Convert `c` to an RGBA color.
123123
124124
If `alpha` is not `None`, it forces the alpha value.
125125
"""
126126
# Special-case nth color syntax because it should not be cached.
127127
if _is_nth_color(c):
128-
from matplotlib import rcParams
129-
prop_cycler = rcParams['axes.prop_cycle']
128+
if prop_cycler is None:
129+
from matplotlib import rcParams
130+
prop_cycler = rcParams['axes.prop_cycle']
130131
colors = prop_cycler.by_key().get('color', ['k'])
131132
c = colors[int(c[1]) % len(colors)]
132133
try:
@@ -211,8 +212,8 @@ def to_rgba_array(c, alpha=None):
211212
# Special-case inputs that are already arrays, for performance. (If the
212213
# array has the wrong kind or shape, raise the error during one-at-a-time
213214
# conversion.)
214-
if (isinstance(c, np.ndarray) and c.dtype.kind in "if"
215-
and c.ndim == 2 and c.shape[1] in [3, 4]):
215+
if (isinstance(c, np.ndarray) and c.dtype.kind in "if" and
216+
c.ndim == 2 and c.shape[1] in [3, 4]):
216217
if c.shape[1] == 3:
217218
result = np.column_stack([c, np.zeros(len(c))])
218219
result[:, -1] = alpha if alpha is not None else 1.
@@ -230,10 +231,10 @@ def to_rgba_array(c, alpha=None):
230231
return result
231232

232233

233-
def to_rgb(c):
234+
def to_rgb(c, prop_cycler=None):
234235
"""Convert `c` to an RGB color, silently dropping the alpha channel.
235236
"""
236-
return to_rgba(c)[:3]
237+
return to_rgba(c, prop_cycler=prop_cycler)[:3]
237238

238239

239240
def to_hex(c, keep_alpha=False):
@@ -248,8 +249,7 @@ def to_hex(c, keep_alpha=False):
248249
return "#" + "".join(format(int(np.round(val * 255)), "02x")
249250
for val in c)
250251

251-
252-
### Backwards-compatible color-conversion API
252+
# ## Backwards-compatible color-conversion API
253253

254254
cnames = CSS4_COLORS
255255
COLOR_NAMES = {'xkcd': XKCD_COLORS, 'css4': CSS4_COLORS}

0 commit comments

Comments
 (0)