Skip to content

Commit

Permalink
Fix for inverted bokeh colormaps and matplotlib Colormap instances
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Mar 28, 2018
1 parent 72f9bf1 commit 2f68e30
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
15 changes: 8 additions & 7 deletions holoviews/plotting/mpl/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,13 +657,14 @@ def _norm_kwargs(self, element, ranges, opts, vdim, prefix=''):
color = color[:-2]
colors[k] = {'color': color, 'alpha': alpha}

if isinstance(cmap, dict):
factors = np.unique(values)
palette = [cmap.get(f, colors.get('NaN', {'color': self._default_nan})['color'])
for f in factors]
else:
palette = process_cmap(cmap, self.color_levels)
cmap = mpl_colors.ListedColormap(palette)
if not isinstance(cmap, mpl_colors.Colormap):
if isinstance(cmap, dict):
factors = np.unique(values)
palette = [cmap.get(f, colors.get('NaN', {'color': self._default_nan})['color'])
for f in factors]
else:
palette = process_cmap(cmap, self.color_levels)
cmap = mpl_colors.ListedColormap(palette)
if 'max' in colors: cmap.set_over(**colors['max'])
if 'min' in colors: cmap.set_under(**colors['min'])
if 'NaN' in colors: cmap.set_bad(**colors['NaN'])
Expand Down
33 changes: 21 additions & 12 deletions holoviews/plotting/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,11 @@ def bokeh_palette_to_palette(cmap, ncolors=None):
from bokeh import palettes

# Handle categorical colormaps to avoid interpolation
categorical = ('category', 'dark', 'colorblind', 'pastel', 'set1', 'set2', 'set3', 'paired')
categorical = ('accent', 'category', 'dark', 'colorblind', 'pastel',
'set1', 'set2', 'set3', 'paired')

reverse = cmap.endswith('_r')
ncolors = ncolors or 256

# Alias mpl tab cmaps with bokeh Category cmaps
if cmap.startswith('tab'):
Expand All @@ -506,21 +510,26 @@ def bokeh_palette_to_palette(cmap, ncolors=None):
if palette is None:
raise ValueError("Supplied palette %s not found among bokeh palettes" % cmap)
elif isinstance(palette, dict):
if ncolors in palette:
if ncolors and ncolors in palette:
palette = palette[ncolors]
if not reverse:
# Bokeh palettes are stored in reverse order
palette = palette[::-1]
elif any(cat in cmap.lower() for cat in categorical):
palette = sorted(palette.items())[-1][1]
else:
palette = sorted(palette.items())[-1][1]
palette = polylinear_gradient(palette, ncolors or 256)
largest_factor = sorted([n for n in palette if ncolors%(n-1) == 0])[-1]
palette = palette[largest_factor]
if not reverse:
# Bokeh palettes are stored in reverse order
palette = palette[::-1]
palette = polylinear_gradient(palette, ncolors)
elif callable(palette):
palette = palette(ncolors or 256)
if ncolors:
return [palette[i%len(palette)] for i in range(ncolors)]
palette = palette(ncolors)
return list(palette)


def linear_gradient(start_hex, finish_hex="#FFFFFF", n=10):
def linear_gradient(start_hex, finish_hex, n=10):
"""
Interpolates the color gradient between to hex colors
"""
Expand All @@ -537,15 +546,15 @@ def polylinear_gradient(colors, n):
"""
Interpolates the color gradients between a list of hex colors.
"""
n_out = int(float(n) / (len(colors) - 1))
n_out = int(float(n) / (len(colors)-1))
gradient = linear_gradient(colors[0], colors[1], n_out)

if len(colors) <= 1:
if len(colors) == len(gradient):
return gradient

for col in range(1, len(colors) - 1):
next_colors = linear_gradient(colors[col], colors[col+1], n_out)
gradient += next_colors[1:]
next_colors = linear_gradient(colors[col], colors[col+1], n_out+1)
gradient += next_colors[1:] if len(next_colors) > 1 else next_colors
return gradient


Expand Down

0 comments on commit 2f68e30

Please sign in to comment.