Skip to content

Commit

Permalink
Bokeh colormapping fixes (#1484)
Browse files Browse the repository at this point in the history
* Fixed bug colormapping BarPlot

* Accept list of colors in place of bokeh cmap

* Add support for explicit cmap colors and bokeh palettes

* Add fix for BarPlot color field sanitization
  • Loading branch information
philippjfr authored and jlstevens committed May 31, 2017
1 parent d5d7d6c commit 12b0815
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
6 changes: 4 additions & 2 deletions holoviews/plotting/bokeh/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,11 +751,11 @@ def get_data(self, element, ranges, empty):

# Get colors
cdim = color_dim or group_dim
cvals = element.dimension_values(cdim) if cdim else None
cvals = element.dimension_values(cdim, expanded=False) if cdim else None
if cvals is not None:
if cvals.dtype.kind in 'if' and no_cidx:
cvals = categorize_array(cvals, group_dim)
factors = None if cvals.dtype.kind in 'if' else list(np.unique(cvals))
factors = None if cvals.dtype.kind in 'if' else list(cvals)
if cdim is xdim and factors:
factors = list(categorize_array(factors, xdim))
if cmap is None and factors:
Expand Down Expand Up @@ -833,6 +833,8 @@ def get_data(self, element, ranges, empty):
for name, val in mapping.items():
if isinstance(val, basestring):
mapping[name] = dimension_sanitizer(mapping[name])
elif isinstance(val, dict) and 'field' in val:
val['field'] = dimension_sanitizer(val['field'])

# Ensure x-values are categorical
xname = dimension_sanitizer(xdim.name)
Expand Down
17 changes: 16 additions & 1 deletion holoviews/plotting/bokeh/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import numpy as np
import bokeh
import bokeh.plotting
from bokeh import palettes
from bokeh.core.properties import value
from bokeh.models import HoverTool, Renderer, Range1d, DataRange1d, FactorRange
from bokeh.models.tickers import Ticker, BasicTicker, FixedTicker, LogTicker
Expand Down Expand Up @@ -1013,7 +1014,21 @@ def _get_colormapper(self, dim, element, ranges, style, factors=None, colors=Non
if colors:
palette = colors
else:
palette = mplcmap_to_palette(style.pop('cmap', 'viridis'), ncolors)
cmap = style.pop('cmap', 'viridis')
if isinstance(cmap, list):
palette = cmap
else:
try:
# Process as matplotlib colormap
palette = mplcmap_to_palette(cmap, ncolors)
except ValueError:
# Process as bokeh palette
palette = getattr(palettes, cmap, None)
if isinstance(palette, dict):
if ncolors in palette:
palette = palette[ncolors]
else:
palette = sorted(palette.items())[-1][1]
nan_colors = {k: rgba_tuple(v) for k, v in self.clipping_colors.items()}
colormapper, opts = self._get_cmapper_opts(low, high, factors, nan_colors)

Expand Down

0 comments on commit 12b0815

Please sign in to comment.