Skip to content

Fix cn bugs #6374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 7, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
FIX: do not directly cache 'CN' color values
  • Loading branch information
tacaswell committed May 6, 2016
commit 6db04da9abce975b1c452be23acdf56c00bf0dba
12 changes: 7 additions & 5 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@
"""
from __future__ import (absolute_import, division, print_function,
unicode_literals)

import re
from matplotlib.externals import six
from matplotlib.externals.six.moves import zip
import warnings
import re

import numpy as np
from numpy import ma
import matplotlib.cbook as cbook
Expand Down Expand Up @@ -194,14 +194,17 @@ def to_rgb(self, arg):
raise ValueError(
'to_rgb: arg "%s" is unhashable even inside a tuple'
% (str(arg),))

try:
if cbook.is_string_like(arg):
argl = arg.lower()
color = self.colors.get(argl, None)
if color is None:
try:
argl = self._parse_nth_color(arg)
# in this case we do not want to cache in case
# the rcparam changes, recurse with the actual color
# value
return self.to_rgb(argl)
except ValueError:
pass
for cmapping in self.CN_LOOKUPS:
Expand All @@ -215,7 +218,7 @@ def to_rgb(self, arg):
if fl < 0 or fl > 1:
raise ValueError(
'gray (string) must be in range 0-1')
color = (fl,)*3
color = (fl,) * 3
elif cbook.iterable(arg):
if len(arg) > 4 or len(arg) < 3:
raise ValueError(
Expand All @@ -228,7 +231,6 @@ def to_rgb(self, arg):
else:
raise ValueError(
'cannot convert argument to rgb sequence')

self.cache[arg] = color

except (KeyError, ValueError, TypeError) as exc:
Expand Down
19 changes: 19 additions & 0 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from numpy.testing.utils import assert_array_equal, assert_array_almost_equal
from nose.plugins.skip import SkipTest

from cycler import cycler
import matplotlib
import matplotlib.colors as mcolors
import matplotlib.cm as cm
import matplotlib.cbook as cbook
Expand Down Expand Up @@ -596,6 +598,23 @@ def test_pandas_iterable():
assert_sequence_equal(cm1.colors, cm2.colors)


@cleanup
def test_cn():
matplotlib.rcParams['axes.prop_cycle'] = cycler('color',
['blue', 'r'])
x11_blue = mcolors.rgb2hex(mcolors.colorConverter.to_rgb('C0'))
assert x11_blue == '#0000ff'
red = mcolors.rgb2hex(mcolors.colorConverter.to_rgb('C1'))
assert red == '#ff0000'

matplotlib.rcParams['axes.prop_cycle'] = cycler('color',
['XKCDblue', 'r'])
XKCD_blue = mcolors.rgb2hex(mcolors.colorConverter.to_rgb('C0'))
assert XKCD_blue == '#0343df'
red = mcolors.rgb2hex(mcolors.colorConverter.to_rgb('C1'))
assert red == '#ff0000'


if __name__ == '__main__':
import nose
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)