Skip to content

Commit

Permalink
Add support for colormapping Contours (#1499)
Browse files Browse the repository at this point in the history
* Add support for colormapping Contours

* Implemented mpl Contour colormapping

* Only colormap bokeh Contours if cmap supplied
  • Loading branch information
philippjfr authored and jlstevens committed May 31, 2017
1 parent 12b0815 commit 0d983ea
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
4 changes: 2 additions & 2 deletions holoviews/plotting/bokeh/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from .chart import (PointPlot, CurvePlot, SpreadPlot, ErrorPlot, HistogramPlot,
SideHistogramPlot, BarPlot, SpikesPlot, SideSpikesPlot,
AreaPlot, VectorFieldPlot)
from .path import PathPlot, PolygonPlot
from .path import PathPlot, PolygonPlot, ContourPlot
from .plot import GridPlot, LayoutPlot, AdjointLayoutPlot
from .raster import (RasterPlot, RGBPlot, HeatmapPlot,
HSVPlot, QuadMeshPlot)
Expand Down Expand Up @@ -66,7 +66,7 @@

# Paths
Path: PathPlot,
Contours: PathPlot,
Contours: ContourPlot,
Path: PathPlot,
Box: PathPlot,
Bounds: PathPlot,
Expand Down
20 changes: 20 additions & 0 deletions holoviews/plotting/bokeh/path.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from collections import defaultdict

import param
import numpy as np

from bokeh.models import HoverTool

Expand Down Expand Up @@ -57,6 +59,24 @@ def get_batched_data(self, element, ranges=None, empty=False):
return data, elmapping


class ContourPlot(ColorbarPlot, PathPlot):

style_opts = line_properties + ['cmap']

def get_data(self, element, ranges=None, empty=False):
data, mapping = super(ContourPlot, self).get_data(element, ranges, empty)
ncontours = len(list(data.values())[0])
style = self.style[self.cyclic_index]
if element.vdims and element.level is not None and 'cmap' in style:
cdim = element.vdims[0]
dim_name = util.dimension_sanitizer(cdim.name)
cmapper = self._get_colormapper(cdim, element, ranges, style)
data[dim_name] = [] if empty else np.full(ncontours, float(element.level))
mapping['line_color'] = {'field': dim_name,
'transform': cmapper}
return data, mapping


class PolygonPlot(ColorbarPlot, PathPlot):

style_opts = ['cmap', 'palette'] + line_properties + fill_properties
Expand Down
2 changes: 1 addition & 1 deletion holoviews/plotting/mpl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def grid_selector(grid):
Text: TextPlot,

# Path plots
Contours: PathPlot,
Contours: ContourPlot,
Path: PathPlot,
Box: PathPlot,
Bounds: PathPlot,
Expand Down
23 changes: 23 additions & 0 deletions holoviews/plotting/mpl/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,29 @@ def update_handles(self, key, axis, element, ranges, style):
return axis_kwargs


class ContourPlot(PathPlot, ColorbarPlot):

style_opts = PathPlot.style_opts + ['cmap']

def get_data(self, element, ranges, style):
args, style, axis_kwargs = super(ContourPlot, self).get_data(element, ranges, style)
value = element.level
if element.vdims and value is not None and np.isfinite(value) and 'cmap' in style:
self._norm_kwargs(element, ranges, style, element.vdims[0])
style['clim'] = style.pop('vmin'), style.pop('vmax')
style['array'] = np.array([value]*len(args[0]))
return args, style, axis_kwargs


def update_handles(self, key, axis, element, ranges, style):
artist = self.handles['artist']
axis_kwargs = super(ContourPlot, self).update_handles(key, axis, element, ranges, style)
if 'array' in style:
artist.set_array(style['array'])
artist.set_clim(style['clim'])
return axis_kwargs


class PolygonPlot(ColorbarPlot):
"""
PolygonPlot draws the polygon paths in the supplied Polygons
Expand Down

0 comments on commit 0d983ea

Please sign in to comment.