Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
prefix = '# ' if comment else ''
data = []
for key, pair in rcdict.items():
# Optionally append description
if description and isinstance(pair, tuple): # add commented out description
value = pair[0]
descrip = '# ' + pair[1]
else:
descrip = ''
if isinstance(pair, tuple):
value = pair[0]
else:
value = pair
# Translate object to string
if isinstance(value, cycler.Cycler): # special case!
value = repr(value)
elif isinstance(value, (str, numbers.Number, NoneType)):
value = str(value)
elif isinstance(value, (list, tuple, np.ndarray)) and all(
isinstance(val, (str, numbers.Number)) for val in value
):
value = ', '.join(str(val) for val in value)
else:
warnings._warn_proplot(
f'Failed to write rc setting {key} = {value!r}. Must be string, '
'number, or list or tuple thereof, or None or a cycler.'
)
continue
if value[:1] == '#': # e.g. HEX string
value = repr(value)
data.append((key, value, descrip))
_color_list = []
for color in palette:
if not is_color_like(color):
# check if the color is a valid R color and translate it
# to a valid hex color value
if color in additional_colors:
color = additional_colors[color]
else:
raise ValueError(
"The following color value of the given palette "
f"is not valid: {color}"
)
_color_list.append(color)
palette = cycler(color=_color_list)
if not isinstance(palette, Cycler):
raise ValueError(
"Please check that the value of 'palette' is a valid "
"matplotlib colormap string (eg. Set2), a list of color names "
"or a cycler with a 'color' key."
)
if 'color' not in palette.keys:
raise ValueError("Please set the palette key 'color'.")
cc = palette()
colors_list = [to_hex(next(cc)['color']) for x in range(len(categories))]
adata.uns[value_to_plot + '_colors'] = colors_list
f'Invalid {key!r} property {value!r}. '
f'Must be list or tuple of properties.'
)
nprops = max(nprops, len(value))
props[key] = [*value] # ensure mutable list
# If args is non-empty, means we want color cycle; otherwise is black
if not args:
props['color'] = [mcolors.to_rgba('k')]
if kwargs:
warnings._warn_proplot(
f'Ignoring Cycle() keyword arg(s) {kwargs}.'
)
# Merge cycler objects
elif all(isinstance(arg, cycler.Cycler) for arg in args):
if kwargs:
warnings._warn_proplot(
f'Ignoring Cycle() keyword arg(s) {kwargs}.'
)
if len(args) == 1:
return args[0]
else:
props = {}
for arg in args:
for key, value in arg.by_key():
if key not in props:
props[key] = []
props[key].extend([*value])
return cycler.cycler(**props)
# Get cycler from a colormap
def default_pal(pal=None):
if pal is None:
return rcParams['axes.prop_cycle']
elif not isinstance(pal, Cycler):
return cycler(color=pal)
def _draw_bars(
cmaps, *, source, unknown='User', categories=None,
length=4.0, width=0.2, N=None
):
"""
Draw colorbars for "colormaps" and "color cycles". This is called by
`show_cycles` and `show_cmaps`.
"""
# Translate local names into database of colormaps
# NOTE: Custom cmap database raises nice error if colormap name is unknown
i = 1
database = pcolors.ColormapDatabase({}) # subset to be drawn
for cmap in cmaps:
if isinstance(cmap, cycler.Cycler):
name = getattr(cmap, 'name', '_no_name')
cmap = mcolors.ListedColormap(cmap.by_key()['color'], name)
elif isinstance(cmap, (list, tuple)):
name = '_no_name'
cmap = mcolors.ListedColormap(cmap, name)
elif isinstance(cmap, mcolors.Colormap):
name = cmap.name
else:
name = cmap
cmap = pcolors._cmap_database[cmap]
if name in database:
name = f'{name}_{i}' # e.g. _no_name_2
i += 1
if name.lower()[-2:] == '_r':
name = name[:-2]
if name.lower()[-2:] == '_s':
if isinstance(palette, list):
islist = True
if (islist and len(palette) < length) or (
not isinstance(palette, list) and len(palette.by_key()["color"]) < length
):
if length <= 28:
palette = palettes.default_26
elif length <= len(palettes.default_64): # 103 colors
palette = palettes.default_64
else:
palette = ["grey" for _ in range(length)]
logg.info("more than 103 colors would be required, initializing as 'grey'")
return palette if islist else cycler(color=palette)
elif islist:
return palette
elif not isinstance(palette, Cycler):
return cycler(color=palette)
else:
return palette
('markersize',markersize),
('markeredgewidth',markeredgewidth),
('markeredgecolor',markeredgecolor),
('markerfacecolor',markerfacecolor),
):
if value is not None:
if isinstance(value, str) or not np.iterable(value):
raise ValueError(f'Invalid {key} property {value}. Must be list or tuple of properties.')
nprops = max(nprops, len(value))
props[key] = [*value] # ensure mutable list
# If args is non-empty, means we want color cycle; otherwise is always black
if not args:
props['color'] = ['k'] # ensures property cycler is non empty
if kwargs:
warnings.warn(f'Ignoring keyword args {kwargs}.')
elif all(isinstance(arg, cycler.Cycler) for arg in args):
# Merge cycler objects
if kwargs:
warnings.warn(f'Ignoring keyword args {kwargs}.')
if len(args) == 1:
return args[0]
else:
props = {}
for arg in args:
for key,value in arg.by_key():
if key not in props:
props[key] = []
props[key].extend([*value])
return cycler.cycler(**props)
else:
# Construct and register ListedColormap
if args and isinstance(args[-1], Number):