Skip to content

Commit 6db04da

Browse files
committed
FIX: do not directly cache 'CN' color values
1 parent 6f47e3e commit 6db04da

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

lib/matplotlib/colors.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@
5454
"""
5555
from __future__ import (absolute_import, division, print_function,
5656
unicode_literals)
57-
57+
import re
5858
from matplotlib.externals import six
5959
from matplotlib.externals.six.moves import zip
6060
import warnings
61-
import re
61+
6262
import numpy as np
6363
from numpy import ma
6464
import matplotlib.cbook as cbook
@@ -194,14 +194,17 @@ def to_rgb(self, arg):
194194
raise ValueError(
195195
'to_rgb: arg "%s" is unhashable even inside a tuple'
196196
% (str(arg),))
197-
198197
try:
199198
if cbook.is_string_like(arg):
200199
argl = arg.lower()
201200
color = self.colors.get(argl, None)
202201
if color is None:
203202
try:
204203
argl = self._parse_nth_color(arg)
204+
# in this case we do not want to cache in case
205+
# the rcparam changes, recurse with the actual color
206+
# value
207+
return self.to_rgb(argl)
205208
except ValueError:
206209
pass
207210
for cmapping in self.CN_LOOKUPS:
@@ -215,7 +218,7 @@ def to_rgb(self, arg):
215218
if fl < 0 or fl > 1:
216219
raise ValueError(
217220
'gray (string) must be in range 0-1')
218-
color = (fl,)*3
221+
color = (fl,) * 3
219222
elif cbook.iterable(arg):
220223
if len(arg) > 4 or len(arg) < 3:
221224
raise ValueError(
@@ -228,7 +231,6 @@ def to_rgb(self, arg):
228231
else:
229232
raise ValueError(
230233
'cannot convert argument to rgb sequence')
231-
232234
self.cache[arg] = color
233235

234236
except (KeyError, ValueError, TypeError) as exc:

lib/matplotlib/tests/test_colors.py

+19
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
from numpy.testing.utils import assert_array_equal, assert_array_almost_equal
1818
from nose.plugins.skip import SkipTest
1919

20+
from cycler import cycler
21+
import matplotlib
2022
import matplotlib.colors as mcolors
2123
import matplotlib.cm as cm
2224
import matplotlib.cbook as cbook
@@ -596,6 +598,23 @@ def test_pandas_iterable():
596598
assert_sequence_equal(cm1.colors, cm2.colors)
597599

598600

601+
@cleanup
602+
def test_cn():
603+
matplotlib.rcParams['axes.prop_cycle'] = cycler('color',
604+
['blue', 'r'])
605+
x11_blue = mcolors.rgb2hex(mcolors.colorConverter.to_rgb('C0'))
606+
assert x11_blue == '#0000ff'
607+
red = mcolors.rgb2hex(mcolors.colorConverter.to_rgb('C1'))
608+
assert red == '#ff0000'
609+
610+
matplotlib.rcParams['axes.prop_cycle'] = cycler('color',
611+
['XKCDblue', 'r'])
612+
XKCD_blue = mcolors.rgb2hex(mcolors.colorConverter.to_rgb('C0'))
613+
assert XKCD_blue == '#0343df'
614+
red = mcolors.rgb2hex(mcolors.colorConverter.to_rgb('C1'))
615+
assert red == '#ff0000'
616+
617+
599618
if __name__ == '__main__':
600619
import nose
601620
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)