Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_init_palette_by_list(self):
"""
Test that a palette can be initialized by a list
"""
# Try all the values in the palettes (HEX)
for value in PALETTES.values():
palette = ColorPalette(value)
self.assertEqual(len(value), len(palette))
# Try all the values converted to RGB
for value in PALETTES.values():
palette = ColorPalette(map(mpl.colors.colorConverter.to_rgb, value))
self.assertEqual(len(value), len(palette))
def test_init_palette_by_name(self):
"""
Test that a palette can be initialized by name
"""
# Try all the names in the palettes
for name, value in PALETTES.items():
try:
palette = ColorPalette(name)
except YellowbrickValueError:
self.fail(
"Could not instantiate {} color palette by name".format(name)
)
self.assertEqual(value, palette)
# Try a name not in PALETTES
with self.assertRaises(YellowbrickValueError):
self.assertNotIn('foo', PALETTES, "Cannot test bad name 'foo' it is in PALETTES!")
palette = ColorPalette('foo')
def test_init_palette_by_list(self):
"""
Test that a palette can be initialized by a list
"""
# Try all the values in the palettes (HEX)
for value in PALETTES.values():
palette = ColorPalette(value)
self.assertEqual(len(value), len(palette))
# Try all the values converted to RGB
for value in PALETTES.values():
palette = ColorPalette(map(mpl.colors.colorConverter.to_rgb, value))
self.assertEqual(len(value), len(palette))
raise YellowbrickValueError(
"'{}' is not a recognized palette!".format(palette)
)
palette = PALETTES[palette.lower()]
if n_colors is None:
n_colors = len(palette)
# Always return as many colors as we asked for
pal_cycle = cycle(palette)
palette = [next(pal_cycle) for _ in range(n_colors)]
# Always return in RGB tuple format
try:
palette = map(mpl.colors.colorConverter.to_rgb, palette)
palette = ColorPalette(palette)
except ValueError:
raise YellowbrickValueError(
"Could not generate a palette for %s" % str(palette)
)
return palette
Parameters
----------
name_or_list :
specify a palette name or a list of RGB or Hex values
"""
if isinstance(name_or_list, str):
if name_or_list not in PALETTES:
raise YellowbrickValueError(
"'{}' is not a recognized palette!".format(name_or_list)
)
name_or_list = PALETTES[name_or_list]
super(ColorPalette, self).__init__(name_or_list)
def as_rgb(self):
"""
Return a color palette with RGB values instead of hex codes.
"""
rgb = [mpl.colors.colorConverter.to_rgb(hex) for hex in self]
return ColorPalette(rgb)
from .palettes import PALETTES, ColorPalette
if isinstance(colormap, str):
try:
# try to get colormap from PALETTES first
_colormap = PALETTES.get(colormap, None)
if _colormap is None:
colormap = cm.get_cmap(colormap)
n_colors = n_colors or len(get_color_cycle())
_colors = list(map(colormap, np.linspace(0, 1, num=n_colors)))
else:
_colors = ColorPalette(_colormap).as_rgb()
n_colors = n_colors or len(_colors)
except ValueError as e:
raise YellowbrickValueError(e)
# if yellowbrick color palette is provided as colormap
elif isinstance(colormap, ColorPalette):
_colors = colormap.as_rgb()
n_colors = n_colors or len(_colors)
# if matplotlib color palette is provided as colormap
elif isinstance(colormap, mpl.colors.Colormap):
n_colors = n_colors or len(get_color_cycle())
_colors = list(map(colormap, np.linspace(0, 1, num=n_colors)))