Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
ncol = _notNone(ncol, 3)
pairs = [pairs[i * ncol:(i + 1) * ncol]
for i in range(len(pairs))] # to list of iterables
if list_of_lists: # remove empty lists, pops up in some examples
pairs = [ipairs for ipairs in pairs if ipairs]
# Now draw legend(s)
legs = []
width, height = self.get_size_inches()
# Individual legend
if not center:
# Optionally change order
# See: https://stackoverflow.com/q/10101141/4970632
# Example: If 5 columns, but final row length 3, columns 0-2 have
# N rows but 3-4 have N-1 rows.
ncol = _notNone(ncol, 3)
if order == 'C':
fpairs = []
# split into rows
split = [pairs[i * ncol:(i + 1) * ncol]
for i in range(len(pairs) // ncol + 1)]
# max possible row count, and columns in final row
nrowsmax, nfinalrow = len(split), len(split[-1])
nrows = [nrowsmax] * nfinalrow + \
[nrowsmax - 1] * (ncol - nfinalrow)
for col, nrow in enumerate(nrows): # iterate through cols
fpairs.extend(split[row][col] for row in range(nrow))
pairs = fpairs
# Make legend object
leg = mlegend.Legend(self, *zip(*pairs), ncol=ncol, loc=loc, **kwargs)
legs = [leg]
# Legend with centered rows, accomplished by drawing separate legends for
# i.e. was a 'values' or 'levels' attribute
elif not isinstance(locator, mticker.Locator):
# Get default maxn, try to allot 2em squares per label maybe?
# NOTE: Cannot use Axes.get_size_inches because this is a
# native matplotlib axes
width, height = self.figure.get_size_inches()
if orientation == 'horizontal':
scale = 3 # em squares alotted for labels
length = width * abs(self.get_position().width)
fontsize = kw_ticklabels.get('size', rc['xtick.labelsize'])
else:
scale = 1
length = height * abs(self.get_position().height)
fontsize = kw_ticklabels.get('size', rc['ytick.labelsize'])
maxn = _notNone(maxn, int(length / (scale * fontsize / 72)))
maxn_minor = _notNone(maxn_minor, int(
length / (0.5 * fontsize / 72)))
# Get locator
if tickminor and minorlocator is None:
step = 1 + len(locator) // max(1, maxn_minor)
minorlocator = locator[::step]
step = 1 + len(locator) // max(1, maxn)
locator = locator[::step]
# Final settings
locator = axistools.Locator(locator, **locator_kw)
formatter = axistools.Formatter(formatter, **formatter_kw)
width, height = self.figure.get_size_inches()
if orientation == 'horizontal':
scale = width * abs(self.get_position().width)
else:
scale = height * abs(self.get_position().height)
# * There is an insanely weird problem with colorbars when simultaneously
# passing levels and norm object to a mappable; fixed by passing
# vmin/vmax instead of levels.
# (see: https://stackoverflow.com/q/40116968/4970632).
# * Problem is often want levels instead of vmin/vmax, while simultaneously
# using a Normalize (for example) to determine colors between the levels
# (see: https://stackoverflow.com/q/42723538/4970632). Workaround makes
# sure locators are in vmin/vmax range exclusively; cannot match values.
# No mutable defaults
locator_kw = locator_kw or {}
minorlocator_kw = minorlocator_kw or {}
formatter_kw = formatter_kw or {}
norm_kw = norm_kw or {}
# Parse flexible input
label = _notNone(title, label, None, names=('title', 'label'))
locator = _notNone(ticks, locator, None, names=('ticks', 'locator'))
formatter = _notNone(
ticklabels, formatter, 'auto',
names=('ticklabels', 'formatter')
)
minorlocator = _notNone(
minorticks, minorlocator, None,
names=('minorticks', 'minorlocator')
)
ticklocation = _notNone(
tickloc, ticklocation, None,
names=('tickloc', 'ticklocation')
)
# Colorbar kwargs
# WARNING: PathCollection scatter objects have an extend method!
grid = _notNone(grid, rc['colorbar.grid'])
c = _notNone(
c, color, markercolor, None,
names=('c', 'color', 'markercolor')
)
s = _notNone(
s, size, markersize, None,
names=('s', 'size', 'markersize')
)
lw = _notNone(
lw, linewidth, linewidths, markeredgewidth, markeredgewidths, None,
names=(
'lw', 'linewidth', 'linewidths',
'markeredgewidth', 'markeredgewidths'
),
)
ec = _notNone(
edgecolor, edgecolors, markeredgecolor, markeredgecolors, None,
names=(
'edgecolor', 'edgecolors', 'markeredgecolor', 'markeredgecolors'
),
)
# Scale s array
if np.iterable(s):
smin_true, smax_true = min(s), max(s)
if smin is None:
smin = smin_true
if smax is None:
smax = smax_true
s = smin + (smax - smin) * (np.array(s) - smin_true) / \
(smax_true - smin_true)
return func(
warnings.warn(f'Ignoring extra kwargs {kwargs}.')
if isinstance(cmap, mcolors.ListedColormap):
if kwargs:
warnings.warn(f'Ignoring extra kwargs {kwargs}.')
elif isinstance(cmap, mcolors.LinearSegmentedColormap):
if kwargs:
warnings.warn(f'Ignoring extra kwargs {kwargs}.')
# Resample, allow overriding the gamma and copy over add-on attribute
cyclic = getattr(cmap, '_cyclic', False)
cmap = cmap._resample(N_) # makes a copy!
cmap._cyclic = cyclic
if isinstance(cmap, PerceptuallyUniformColormap):
cmap._gamma1 = _notNone(gamma1, gamma, cmap._gamma1)
cmap._gamma2 = _notNone(gamma2, gamma, cmap._gamma2)
elif gamma:
cmap._gamma = _notNone(gamma, cmap._gamma)
# Build colormap on-the-fly
elif isinstance(cmap, dict):
# Dictionary of hue/sat/luminance values or 2-tuples representing linear transition
try:
cmap = PerceptuallyUniformColormap.from_hsl(name, N=N_, **cmap, **kwargs)
except Exception:
raise ValueError(f'Invalid input "{cmap}" for from_hsl method.')
elif not isinstance(cmap, str) and np.iterable(cmap) and all(np.iterable(color) for color in cmap):
# List of color tuples or color strings, i.e. iterable of iterables
cmap = [to_rgb(color, cycle=cycle) for color in cmap] # transform C0, C1, etc. to actual names
if listmode == 'listed':
cmap = mcolors.ListedColormap(cmap, name=name, **kwargs)
elif listmode == 'linear':
cmap = mcolors.LinearSegmentedColormap.from_list(name, cmap, **kwargs)
else:
cmap = PerceptuallyUniformColormap.from_list(name, cmap, **kwargs)
cmap_kw = cmap_kw or {}
norm_kw = norm_kw or {}
locator_kw = locator_kw or {}
labels_kw = labels_kw or {}
colorbar_kw = colorbar_kw or {}
# Parse args
# Disable edgefix=True for certain keyword combos e.g. if user wants
# white lines around their pcolor mesh.
name = func.__name__
if not args:
return func(self, *args, **kwargs)
vmin = _notNone(
vmin, norm_kw.pop('vmin', None), None,
names=('vmin', 'norm_kw={"vmin":value}'))
vmax = _notNone(
vmax, norm_kw.pop('vmax', None), None,
names=('vmax', 'norm_kw={"vmax":value}'))
levels = _notNone(
N, levels, norm_kw.pop('levels', None), rc['image.levels'],
names=('N', 'levels', 'norm_kw={"levels":value}'))
values = _notNone(
values, centers, None,
names=('values', 'centers'))
colors = _notNone(
color, colors, edgecolor, edgecolors, None,
names=('color', 'colors', 'edgecolor', 'edgecolors'))
linewidths = _notNone(
lw, linewidth, linewidths, None,
names=('lw', 'linewidth', 'linewidths'))
linestyles = _notNone(
ls, linestyle, linestyles, None,
N, levels, norm_kw.pop('levels', None), rc['image.levels'],
names=('N', 'levels', 'norm_kw={"levels":value}'))
values = _notNone(
values, centers, None,
names=('values', 'centers'))
colors = _notNone(
color, colors, edgecolor, edgecolors, None,
names=('color', 'colors', 'edgecolor', 'edgecolors'))
linewidths = _notNone(
lw, linewidth, linewidths, None,
names=('lw', 'linewidth', 'linewidths'))
linestyles = _notNone(
ls, linestyle, linestyles, None,
names=('ls', 'linestyle', 'linestyles'))
style_kw = STYLE_ARGS_TRANSLATE.get(name, {})
edgefix = _notNone(edgefix, rc['image.edgefix'])
for key, value in (
('colors', colors),
('linewidths', linewidths),
('linestyles', linestyles)):
if value is None:
continue
elif 'contourf' in name: # special case, we re-draw our own contours
continue
if key in style_kw:
edgefix = False # override!
kwargs[style_kw[key]] = value
else:
raise ValueError(
f'Unknown keyword arg {key!r} for function {name!r}.'
)
# Check input
cmap = PerceptuallyUniformColormap.from_list(name, cmap, **kwargs)
else:
# Monochrome colormap from input color
if isinstance(cmap, str) and cmap[-2:] == '_r':
cmap = cmap[:-2]
ireverse = (not ireverse)
try:
color = to_rgb(cmap, cycle=cycle)
except Exception:
if not isinstance(cmap, str):
raise ValueError(f'Invalid cmap, cycle, or color "{cmap}".')
else:
raise ValueError(f'Invalid cmap, cycle, or color "{cmap}".\n'
f'VALID CMAP AND CYCLE NAMES: {", ".join(sorted(mcm.cmap_d))}.\n'
f'VALID COLOR NAMES: {", ".join(sorted(mcolors.colorConverter.colors.keys()))}.')
fade = _notNone(fade, 100)
cmap = monochrome_cmap(color, fade, name=name, N=N_, **kwargs)
# Optionally transform colormap by clipping colors or reversing
if ireverse:
cmap = cmap.reversed()
cmap = _slice_cmap(cmap, None if not np.iterable(left) else left[i],
None if not np.iterable(right) else right[i], N=N)
imaps += [cmap]
# Now merge the result of this arbitrary user input
# Since we are merging cmaps, potentially *many* color transitions; use big number by default
N_ = N_*len(imaps)
if len(imaps) > 1:
cmap = _merge_cmaps(*imaps, name=name, ratios=ratios, N=N_)
# Cut out either edge
formatter = _notNone(
ticklabels, formatter, 'auto',
names=('ticklabels', 'formatter')
)
minorlocator = _notNone(
minorticks, minorlocator, None,
names=('minorticks', 'minorlocator')
)
ticklocation = _notNone(
tickloc, ticklocation, None,
names=('tickloc', 'ticklocation')
)
# Colorbar kwargs
# WARNING: PathCollection scatter objects have an extend method!
grid = _notNone(grid, rc['colorbar.grid'])
if extend is None:
if isinstance(getattr(mappable, 'extend', None), str):
extend = mappable.extend or 'neither'
else:
extend = 'neither'
kwargs.update({
'cax': self,
'use_gridspec': True,
'orientation': orientation,
'extend': extend,
'spacing': 'uniform'
})
kwargs.setdefault('drawedges', grid)
# Text property keyword args
kw_label = {}