Skip to content

Commit 1f6e618

Browse files
committed
Add leading underscore to all default colormap names
1 parent 4988035 commit 1f6e618

1 file changed

Lines changed: 59 additions & 25 deletions

File tree

proplot/colors.py

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,14 @@ def _warn_or_raise(descrip, error=RuntimeError):
891891
data = [(x, color) for x, color in zip(x, data)]
892892
return ContinuousColormap.from_list(name, data)
893893

894+
@property
895+
def _copy_name(self):
896+
# The name used when making copies
897+
name = self.name or ''
898+
if name and name[0] != '_':
899+
name = '_' + name
900+
return name + '_copy'
901+
894902

895903
class ContinuousColormap(mcolors.LinearSegmentedColormap, _Colormap):
896904
r"""
@@ -1012,7 +1020,7 @@ def append(self, *args, ratios=None, name=None, N=None, **kwargs):
10121020
# we never interpolate between end colors of different colormaps
10131021
segmentdata = {}
10141022
if name is None:
1015-
name = '_'.join(cmap.name for cmap in cmaps)
1023+
name = '_' + '_'.join(cmap.name for cmap in cmaps)
10161024
if not np.iterable(ratios):
10171025
ratios = [1] * len(cmaps)
10181026
ratios = np.asarray(ratios) / np.sum(ratios)
@@ -1101,7 +1109,7 @@ def cut(self, cut=None, name=None, left=None, right=None, **kwargs):
11011109
``cut=0.1`` cuts the central 10%, or ``cut=-0.1`` fills the ctranl 10%
11021110
of the colormap with the current central color (usually white).
11031111
name : str, optional
1104-
The name of the new colormap. Default is ``self.name + '_copy'``.
1112+
The name of the new colormap. Default is ``'_name_copy'``.
11051113
left, right : float, optional
11061114
The colormap indices for the "leftmost" and "rightmost" colors.
11071115
Defaults are ``0`` and ``1``. See
@@ -1140,7 +1148,7 @@ def cut(self, cut=None, name=None, left=None, right=None, **kwargs):
11401148
f'Invalid combination cut={cut} for left={left} and right={right}.'
11411149
)
11421150
if name is None:
1143-
name = self.name + '_copy'
1151+
name = self._copy_name
11441152
cmap_left = self.truncate(left, 0.5 - offset)
11451153
cmap_right = self.truncate(0.5 + offset, right)
11461154

@@ -1167,7 +1175,7 @@ def reversed(self, name=None, **kwargs):
11671175
Parameters
11681176
----------
11691177
name : str, optional
1170-
The name of the new colormap. Default is ``self.name + '_r'``.
1178+
The name of the new colormap. Default is ``'_name_r'``.
11711179
11721180
Other parameters
11731181
----------------
@@ -1197,7 +1205,7 @@ def reversed(self, name=None, **kwargs):
11971205
if gamma is not None and np.iterable(gamma):
11981206
kwargs[key] = gamma[::-1]
11991207
if name is None:
1200-
name = self.name + '_r'
1208+
name = '_' + self.name + '_r'
12011209

12021210
cmap = self.copy(name, segmentdata, **kwargs)
12031211
cmap._rgba_under, cmap._rgba_over = cmap._rgba_over, cmap._rgba_under
@@ -1303,7 +1311,7 @@ def shifted(self, shift=180, name=None, **kwargs):
13031311
The number of degrees to shift, out of 360 degrees.
13041312
The default is ``180``.
13051313
name : str, optional
1306-
The name of the new colormap. Default is ``self.name + '_s'``.
1314+
The name of the new colormap. Default is ``'_name_s'``.
13071315
13081316
Other parameters
13091317
----------------
@@ -1318,7 +1326,7 @@ def shifted(self, shift=180, name=None, **kwargs):
13181326
if shift == 0:
13191327
return self
13201328
if name is None:
1321-
name = self.name + '_s'
1329+
name = '_' + self.name + '_s'
13221330
if not self._cyclic:
13231331
warnings._warn_proplot(
13241332
f'Shifting non-cyclic colormap {self.name!r}. Use cmap.set_cyclic(True)'
@@ -1346,7 +1354,7 @@ def truncate(self, left=None, right=None, name=None, **kwargs):
13461354
The colormap index for the new "rightmost" color. Must fall between ``0``
13471355
and ``1``. For example, ``right=0.9`` cuts the leftmost 10%% of the colors.
13481356
name : str, optional
1349-
The name of the new colormap. Default is ``self.name + '_copy'``.
1357+
The name of the new colormap. Default is ``'_name_copy'``.
13501358
13511359
Other parameters
13521360
----------------
@@ -1364,7 +1372,7 @@ def truncate(self, left=None, right=None, name=None, **kwargs):
13641372
if left == 0 and right == 1:
13651373
return self
13661374
if name is None:
1367-
name = self.name + '_copy'
1375+
name = self._copy_name
13681376

13691377
# Resample the segmentdata arrays
13701378
segmentdata = {}
@@ -1436,7 +1444,7 @@ def copy(
14361444
Parameters
14371445
----------
14381446
name : str
1439-
The name of the new colormap. Default is ``self.name + '_copy'``.
1447+
The name of the new colormap. Default is ``'_name_copy'``.
14401448
segmentdata, N, alpha, gamma, cyclic : optional
14411449
See `ContinuousColormap`. If not provided, these are copied from
14421450
the current colormap.
@@ -1447,7 +1455,7 @@ def copy(
14471455
PerceptualColormap.copy
14481456
"""
14491457
if name is None:
1450-
name = self.name + '_copy'
1458+
name = self._copy_name
14511459
if segmentdata is None:
14521460
segmentdata = self._segmentdata.copy()
14531461
if gamma is None:
@@ -1477,7 +1485,7 @@ def to_discrete(self, samples=10, name=None, **kwargs):
14771485
``np.linspace(0, 1, samples)``. If sequence of float,
14781486
draw samples at the specified points.
14791487
name : str, optional
1480-
The name of the new colormap. Default is ``self.name + '_copy'``.
1488+
The name of the new colormap. Default is ``'_name_copy'``.
14811489
14821490
Other parameters
14831491
----------------
@@ -1495,7 +1503,7 @@ def to_discrete(self, samples=10, name=None, **kwargs):
14951503
samples = np.asarray(samples)
14961504
colors = self(samples)
14971505
if name is None:
1498-
name = self.name + '_copy'
1506+
name = self._copy_name
14991507
return DiscreteColormap(colors, name=name, **kwargs)
15001508

15011509
@classmethod
@@ -1661,9 +1669,10 @@ def append(self, *args, name=None, N=None, **kwargs):
16611669
raise TypeError(f'Arguments {args!r} must be DiscreteColormap.')
16621670
cmaps = (self, *args)
16631671
if name is None:
1664-
name = '_'.join(cmap.name for cmap in cmaps)
1672+
name = '_' + '_'.join(cmap.name for cmap in cmaps)
16651673
colors = [color for cmap in cmaps for color in cmap.colors]
1666-
return self.copy(colors, name, N or len(colors), **kwargs)
1674+
N = _not_none(N, len(colors))
1675+
return self.copy(colors, name, N, **kwargs)
16671676

16681677
@docstring._snippet_manager
16691678
def save(self, path=None, alpha=True):
@@ -1711,6 +1720,31 @@ def set_alpha(self, alpha):
17111720
self.colors = [set_alpha(color, alpha) for color in self.colors]
17121721
self._init()
17131722

1723+
def reversed(self, name=None, **kwargs):
1724+
"""
1725+
Return a reversed version of the colormap.
1726+
1727+
Parameters
1728+
----------
1729+
name : str, optional
1730+
The name of the new colormap. Default is ``'_name_r'``.
1731+
1732+
Other parameters
1733+
----------------
1734+
**kwargs
1735+
Passed to `DiscreteColormap.copy`
1736+
1737+
See also
1738+
--------
1739+
matplotlib.colors.ListedColormap.reversed
1740+
"""
1741+
if name is None:
1742+
name = '_' + self.name + '_r'
1743+
colors = self.colors[::-1]
1744+
cmap = self.copy(colors, name, **kwargs)
1745+
cmap._rgba_under, cmap._rgba_over = cmap._rgba_over, cmap._rgba_under
1746+
return cmap
1747+
17141748
def shifted(self, shift=1, name=None):
17151749
"""
17161750
Return a cyclically shifted version of the colormap.
@@ -1721,7 +1755,7 @@ def shifted(self, shift=1, name=None):
17211755
The number of places to shift, between ``-self.N`` and ``self.N``.
17221756
The default is ``1``.
17231757
name : str, optional
1724-
The name of the new colormap. Default is ``self.name + '_s'``.
1758+
The name of the new colormap. Default is ``'_name_s'``.
17251759
17261760
See also
17271761
--------
@@ -1730,7 +1764,7 @@ def shifted(self, shift=1, name=None):
17301764
if not shift:
17311765
return self
17321766
if name is None:
1733-
name = self.name + '_s'
1767+
name = '_' + self.name + '_s'
17341768
shift = shift % len(self.colors)
17351769
colors = list(self.colors)
17361770
colors = colors[shift:] + colors[:shift]
@@ -1751,7 +1785,7 @@ def truncate(self, left=None, right=None, name=None):
17511785
``0`` and ``self.N``. For example,
17521786
``right=4`` deletes colors after the fourth color.
17531787
name : str, optional
1754-
The name of the new colormap. Default is ``self.name + '_copy'``.
1788+
The name of the new colormap. Default is ``'_name_copy'``.
17551789
17561790
See also
17571791
--------
@@ -1760,7 +1794,7 @@ def truncate(self, left=None, right=None, name=None):
17601794
if left is None and right is None:
17611795
return self
17621796
if name is None:
1763-
name = self.name + '_copy'
1797+
name = self._copy_name
17641798
colors = self.colors[left:right]
17651799
return self.copy(colors, name, len(colors))
17661800

@@ -1772,7 +1806,7 @@ def copy(self, colors=None, name=None, N=None, *, alpha=None):
17721806
Parameters
17731807
----------
17741808
name : str
1775-
The name of the new colormap. Default is ``self.name + '_copy'``.
1809+
The name of the new colormap. Default is ``'_name_copy'``.
17761810
colors, N, alpha : optional
17771811
See `DiscreteColormap`. If not provided,
17781812
these are copied from the current colormap.
@@ -1783,7 +1817,7 @@ def copy(self, colors=None, name=None, N=None, *, alpha=None):
17831817
PerceptualColormap.copy
17841818
"""
17851819
if name is None:
1786-
name = self.name + '_copy'
1820+
name = self._copy_name
17871821
if colors is None:
17881822
colors = list(self.colors) # copy
17891823
if N is None:
@@ -1965,7 +1999,7 @@ def copy(
19651999
Parameters
19662000
----------
19672001
name : str
1968-
The name of the new colormap. Default is ``self.name + '_copy'``.
2002+
The name of the new colormap. Default is ``'_name_copy'``.
19692003
segmentdata, N, alpha, clip, cyclic, gamma, gamma1, gamma2, space : optional
19702004
See `PerceptualColormap`. If not provided,
19712005
these are copied from the current colormap.
@@ -1976,7 +2010,7 @@ def copy(
19762010
ContinuousColormap.copy
19772011
"""
19782012
if name is None:
1979-
name = self.name + '_copy'
2013+
name = self._copy_name
19802014
if segmentdata is None:
19812015
segmentdata = self._segmentdata.copy()
19822016
if space is None:
@@ -2011,7 +2045,7 @@ def to_continuous(self, name=None, **kwargs):
20112045
Parameters
20122046
----------
20132047
name : str, optional
2014-
The name of the new colormap. Default is ``self.name + '_copy'``.
2048+
The name of the new colormap. Default is ``'_name_copy'``.
20152049
20162050
Other parameters
20172051
----------------
@@ -2025,7 +2059,7 @@ def to_continuous(self, name=None, **kwargs):
20252059
if not self._isinit:
20262060
self._init()
20272061
if name is None:
2028-
name = self.name + '_copy'
2062+
name = self._copy_name
20292063
return ContinuousColormap.from_list(name, self._lut[:-3, :], **kwargs)
20302064

20312065
@classmethod

0 commit comments

Comments
 (0)