Skip to content

Commit

Permalink
Add color bar limit opt and redim range if values are equal. (#3382)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuang11 authored and philippjfr committed Jan 15, 2019
1 parent ff58957 commit 35ce68c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
15 changes: 14 additions & 1 deletion holoviews/plotting/bokeh/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,10 @@ class ColorbarPlot(ElementPlot):
Number of discrete colors to use when colormapping or a set of color
intervals defining the range of values to map each color to.""")

clim = param.NumericTuple(default=(np.nan, np.nan), length=2, doc="""
User-specified colorbar axis range limits for the plot, as a tuple (low,high).
If specified, takes precedence over data and dimension ranges.""")

colorbar = param.Boolean(default=False, doc="""
Whether to display a colorbar.""")

Expand Down Expand Up @@ -1405,7 +1409,10 @@ def _get_colormapper(self, eldim, element, ranges, style, factors=None, colors=N

ncolors = None if factors is None else len(factors)
if eldim:
if dim_name in ranges:
# check if there's an actual value (not np.nan)
if util.isfinite(self.clim).all():
low, high = self.clim
elif dim_name in ranges:
low, high = ranges[dim_name]['combined']
elif isinstance(eldim, dim):
low, high = np.nan, np.nan
Expand Down Expand Up @@ -1503,6 +1510,7 @@ def _get_color_data(self, element, ranges, style, name='color', factors=None, co
if factors is not None and self.show_legend:
mapping['legend'] = {'field': field}
mapping[name] = {'field': field, 'transform': mapper}

return data, mapping


Expand All @@ -1511,6 +1519,11 @@ def _get_cmapper_opts(self, low, high, factors, colors):
colormapper = LogColorMapper if self.logz else LinearColorMapper
if isinstance(low, (bool, np.bool_)): low = int(low)
if isinstance(high, (bool, np.bool_)): high = int(high)
# Pad zero-range to avoid breaking colorbar (as of bokeh 1.0.4)
if low == high:
offset = self.default_span / 2
low -= offset
high += offset
opts = {}
if util.isfinite(low):
opts['low'] = low
Expand Down
9 changes: 9 additions & 0 deletions holoviews/plotting/mpl/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,10 @@ def teardown_handles(self):

class ColorbarPlot(ElementPlot):

clim = param.NumericTuple(default=(np.nan, np.nan), length=2, doc="""
User-specified colorbar axis range limits for the plot, as a tuple (low,high).
If specified, takes precedence over data and dimension ranges.""")

colorbar = param.Boolean(default=False, doc="""
Whether to draw a colorbar.""")

Expand Down Expand Up @@ -775,6 +779,11 @@ def _norm_kwargs(self, element, ranges, opts, vdim, values=None, prefix=''):
self.handles[prefix+'color_dim'] = vdim

clim = opts.pop(prefix+'clims', None)

# check if there's an actual value (not np.nan)
if clim is None and util.isfinite(self.clim).all():
clim = self.clim

if clim is None:
if not len(values):
clim = (0, 0)
Expand Down
8 changes: 7 additions & 1 deletion holoviews/plotting/plotly/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,10 @@ def update_frame(self, key, ranges=None, element=None):

class ColorbarPlot(ElementPlot):

clim = param.NumericTuple(default=(np.nan, np.nan), length=2, doc="""
User-specified colorbar axis range limits for the plot, as a tuple (low,high).
If specified, takes precedence over data and dimension ranges.""")

colorbar = param.Boolean(default=False, doc="""
Whether to display a colorbar.""")

Expand Down Expand Up @@ -390,7 +394,9 @@ def get_color_opts(self, eldim, element, ranges, style):

if eldim:
auto = False
if dim_name in ranges:
if util.isfinite(self.clim).all():
cmin, cmax = self.clim
elif dim_name in ranges:
cmin, cmax = ranges[dim_name]['combined']
elif isinstance(eldim, dim):
cmin, cmax = np.nan, np.nan
Expand Down

0 comments on commit 35ce68c

Please sign in to comment.