Skip to content

Commit

Permalink
Fix for non-categorical discrete color levels in bokeh (#2808)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr authored and jlstevens committed Jun 20, 2018
1 parent 502e9d3 commit f4c7477
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
3 changes: 2 additions & 1 deletion holoviews/plotting/bokeh/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,7 @@ def _get_colormapper(self, dim, element, ranges, style, factors=None, colors=Non
if isinstance(cmap, dict) and factors:
palette = [cmap.get(f, nan_colors.get('NaN', self._default_nan)) for f in factors]
else:
categorical = ncolors is not None
if isinstance(self.color_levels, int):
ncolors = self.color_levels
elif isinstance(self.color_levels, list):
Expand All @@ -1089,7 +1090,7 @@ def _get_colormapper(self, dim, element, ranges, style, factors=None, colors=Non
'must match the intervals defined in the '
'color_levels, expected %d colors found %d.'
% (ncolors, len(cmap)))
palette = process_cmap(cmap, ncolors, categorical=ncolors is not None)
palette = process_cmap(cmap, ncolors, categorical=categorical)
if isinstance(self.color_levels, list):
palette = color_intervals(palette, self.color_levels, clip=(low, high))
colormapper, opts = self._get_cmapper_opts(low, high, factors, nan_colors)
Expand Down
7 changes: 5 additions & 2 deletions tests/plotting/bokeh/testelementplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from holoviews.core import Dimension, DynamicMap
from holoviews.element import Curve, Image, Scatter, Labels
from holoviews.streams import Stream
from holoviews.plotting.util import process_cmap

from .testplot import TestBokehPlot, bokeh_renderer

Expand Down Expand Up @@ -150,12 +151,14 @@ def test_colormapper_symmetric(self):
cmapper = plot.handles['color_mapper']
self.assertEqual(cmapper.low, -3)
self.assertEqual(cmapper.high, 3)

def test_colormapper_color_levels(self):
img = Image(np.array([[0, 1], [2, 3]])).options(color_levels=5)
cmap = process_cmap('viridis', provider='bokeh')
img = Image(np.array([[0, 1], [2, 3]])).options(color_levels=5, cmap=cmap)
plot = bokeh_renderer.get_plot(img)
cmapper = plot.handles['color_mapper']
self.assertEqual(len(cmapper.palette), 5)
self.assertEqual(cmapper.palette, ['#440154', '#440255', '#440357', '#450558', '#45065A'])

def test_colormapper_transparent_nan(self):
img = Image(np.array([[0, 1], [2, 3]])).options(clipping_colors={'NaN': 'transparent'})
Expand Down

0 comments on commit f4c7477

Please sign in to comment.