Skip to content

Commit e8214b3

Browse files
committed
Fix issues merging PerceptuallyUniform w/ LinearSegmented
1 parent eca01b9 commit e8214b3

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

proplot/colors.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,8 @@ def append(self, *args, ratios=None, name=None, N=None, **kwargs):
657657
# PerceptuallyUniformColormap --> LinearSegmentedColormap conversions
658658
cmaps = [self, *args]
659659
spaces = {getattr(cmap, '_space', None) for cmap in cmaps}
660-
if len(spaces) > 1: # mixed colorspaces *or* mixed types
660+
to_linear_segmented = len(spaces) > 1 # mixed colorspaces *or* mixed types
661+
if to_linear_segmented:
661662
for i, cmap in enumerate(cmaps):
662663
if isinstance(cmap, PerceptuallyUniformColormap):
663664
cmaps[i] = cmap.to_linear_segmented()
@@ -672,7 +673,7 @@ def append(self, *args, ratios=None, name=None, N=None, **kwargs):
672673
ratios = np.asarray(ratios) / np.sum(ratios)
673674
x0 = np.append(0, np.cumsum(ratios)) # coordinates for edges
674675
xw = x0[1:] - x0[:-1] # widths between edges
675-
for key in self._segmentdata.keys():
676+
for key in cmaps[0]._segmentdata.keys(): # not self._segmentdata
676677
# Callable segments
677678
# WARNING: If just reference a global 'funcs' list from inside the
678679
# 'data' function it can get overwritten in this loop. Must
@@ -688,6 +689,7 @@ def xyy(ix, funcs=funcs):
688689
idx = max(np.searchsorted(x0, jx) - 1, 0)
689690
kx.flat[j] = funcs[idx]((jx - x0[idx]) / xw[idx])
690691
return kx
692+
691693
# Concatenate segment arrays and make the transition at the
692694
# seam instant so we *never interpolate* between end colors
693695
# of different maps.
@@ -702,11 +704,13 @@ def xyy(ix, funcs=funcs):
702704
datas[i + 1] = datas[i + 1][1:, :]
703705
xyy = np.concatenate(datas, axis=0)
704706
xyy[:, 0] = xyy[:, 0] / xyy[:, 0].max(axis=0) # fix fp errors
707+
705708
else:
706709
raise TypeError(
707710
'Mixed callable and non-callable colormap values.'
708711
)
709712
segmentdata[key] = xyy
713+
710714
# Handle gamma values
711715
if key == 'saturation':
712716
ikey = 'gamma1'
@@ -717,6 +721,7 @@ def xyy(ix, funcs=funcs):
717721
if ikey in kwargs:
718722
continue
719723
gamma = []
724+
720725
for cmap in cmaps:
721726
igamma = getattr(cmap, '_' + ikey)
722727
if not np.iterable(igamma):
@@ -725,6 +730,7 @@ def xyy(ix, funcs=funcs):
725730
else:
726731
igamma = (len(cmap._segmentdata[key]) - 1) * [igamma]
727732
gamma.extend(igamma)
733+
728734
if all(callable_):
729735
if any(igamma != gamma[0] for igamma in gamma[1:]):
730736
warnings._warn_proplot(
@@ -733,10 +739,14 @@ def xyy(ix, funcs=funcs):
733739
f'gamma of {gamma[0]}.'
734740
)
735741
gamma = gamma[0]
742+
736743
kwargs[ikey] = gamma
737744

738-
# Return copy
739-
return self.copy(name=name, segmentdata=segmentdata, N=N, **kwargs)
745+
# Return copy or merge mixed types
746+
if to_linear_segmented and isinstance(self, PerceptuallyUniformColormap):
747+
return LinearSegmentedColormap(name, segmentdata, N, **kwargs)
748+
else:
749+
return self.copy(name, segmentdata, N, **kwargs)
740750

741751
def cut(self, cut=None, name=None, left=None, right=None, **kwargs):
742752
"""

0 commit comments

Comments
 (0)