Skip to content

Commit

Permalink
Correctly resample colorcet colormaps
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Sep 20, 2019
1 parent b20132f commit e49a784
Showing 1 changed file with 35 additions and 16 deletions.
51 changes: 35 additions & 16 deletions holoviews/plotting/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,20 @@ def map_colors(arr, crange, cmap, hex=True):
return arr


def resample_palette(palette, ncolors, categorical, cmap_categorical):
"""
Resample the number of colors in a palette to the selected number.
"""
if len(palette) != ncolors:
if categorical and cmap_categorical:
palette = [palette[i%len(palette)] for i in range(ncolors)]
else:
lpad, rpad = -0.5, 0.49999999999
indexes = np.linspace(lpad, (len(palette)-1)+rpad, ncolors)
palette = [palette[int(np.round(v))] for v in indexes]
return palette


def mplcmap_to_palette(cmap, ncolors=None, categorical=False):
"""
Converts a matplotlib colormap to palette of RGB hex strings."
Expand Down Expand Up @@ -550,6 +564,22 @@ def mplcmap_to_palette(cmap, ncolors=None, categorical=False):
return [rgb2hex(c) for c in cmap(np.linspace(0, 1, ncolors))]


def colorcet_cmap_to_palette(cmap, ncolors=None, categorical=False):
from colorcet import palette

categories = ['glasbey']

ncolors = ncolors or 256
cmap_categorical = any(c in cmap for c in categories)

if cmap.endswith('_r'):
palette = list(reversed(palette[cmap[:-2]]))
else:
palette = palette[cmap]

return resample_palette(palette, ncolors, categorical, cmap_categorical)


def bokeh_palette_to_palette(cmap, ncolors=None, categorical=False):
from bokeh import palettes

Expand Down Expand Up @@ -589,14 +619,7 @@ def bokeh_palette_to_palette(cmap, ncolors=None, categorical=False):
palette = palette(ncolors)
if reverse: palette = palette[::-1]

if len(palette) != ncolors:
if categorical and cmap_categorical:
palette = [palette[i%len(palette)] for i in range(ncolors)]
else:
lpad, rpad = -0.5, 0.49999999999
indexes = np.linspace(lpad, (len(palette)-1)+rpad, ncolors)
palette = [palette[int(np.round(v))] for v in indexes]
return palette
return resample_palette(palette, ncolors, categorical, cmap_categorical)


def linear_gradient(start_hex, finish_hex, n=10):
Expand Down Expand Up @@ -865,16 +888,12 @@ def process_cmap(cmap, ncolors=None, provider=None, categorical=False):
mpl_cmaps = _list_cmaps('matplotlib')
bk_cmaps = _list_cmaps('bokeh')
cet_cmaps = _list_cmaps('colorcet')
if provider=='matplotlib' or (provider is None and (cmap in mpl_cmaps or cmap.lower() in mpl_cmaps)):
if provider == 'matplotlib' or (provider is None and (cmap in mpl_cmaps or cmap.lower() in mpl_cmaps)):
palette = mplcmap_to_palette(cmap, ncolors, categorical)
elif provider=='bokeh' or (provider is None and (cmap in bk_cmaps or cmap.capitalize() in bk_cmaps)):
elif provider == 'bokeh' or (provider is None and (cmap in bk_cmaps or cmap.capitalize() in bk_cmaps)):
palette = bokeh_palette_to_palette(cmap, ncolors, categorical)
elif provider=='colorcet' or (provider is None and cmap in cet_cmaps):
from colorcet import palette
if cmap.endswith('_r'):
palette = list(reversed(palette[cmap[:-2]]))
else:
palette = palette[cmap]
elif provider == 'colorcet' or (provider is None and cmap in cet_cmaps):
palette = colorcet_cmap_to_palette(cmap, ncolors, categorical)
else:
raise ValueError("Supplied cmap %s not found among %s colormaps." %
(cmap,providers_checked))
Expand Down

0 comments on commit e49a784

Please sign in to comment.