Skip to content

Commit

Permalink
Detecting color clipping and added control over clipping_colors
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed May 13, 2016
1 parent 5b6b9af commit 5c7b351
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
38 changes: 34 additions & 4 deletions holoviews/plotting/mpl/element.py
@@ -1,4 +1,4 @@
import math
import math, copy

from matplotlib import ticker
from matplotlib import colors
Expand Down Expand Up @@ -497,8 +497,10 @@ class ColorbarPlot(ElementPlot):
colorbar = param.Boolean(default=False, doc="""
Whether to draw a colorbar.""")

cbar_width = param.Number(default=0.05, doc="""
Width of the colorbar as a fraction of the main plot""")
clipping_colors = param.Dict(default={'NaN': ('w', 1)}, doc="""
Dictionary to specify colors for clipped values, allows setting
color for NaN values and for values above and below the min and
max value.""")

cbar_padding = param.Number(default=0.01, doc="""
Padding between colorbar and other plots.""")
Expand All @@ -510,11 +512,18 @@ class ColorbarPlot(ElementPlot):
set to None default matplotlib ticking behavior is
applied.""")

cbar_width = param.Number(default=0.05, doc="""
Width of the colorbar as a fraction of the main plot""")

symmetric = param.Boolean(default=False, doc="""
Whether to make the colormap symmetric around zero.""")

_colorbars = {}

def __init__(self, *args, **kwargs):
super(ColorbarPlot, self).__init__(*args, **kwargs)
self._cbar_extend = 'neither'

def _adjust_cbar(self, cbar, label, dim):
noalpha = math.floor(self.style[self.cyclic_index].get('alpha', 1)) == 1
if (cbar.solids and noalpha):
Expand Down Expand Up @@ -571,7 +580,7 @@ def _draw_colorbar(self, artist, element, dim=None):
scaled_w = w*width
cax = fig.add_axes([l+w+padding+(scaled_w+padding+w*0.15)*offset,
b, scaled_w, h])
cbar = plt.colorbar(artist, cax=cax)
cbar = plt.colorbar(artist, cax=cax, extend=self._cbar_extend)
self._adjust_cbar(cbar, label, dim)
self.handles['cax'] = cax
self.handles['cbar'] = cbar
Expand Down Expand Up @@ -608,6 +617,27 @@ def _norm_kwargs(self, element, ranges, opts, vdim):
opts['vmin'] = clim[0]
opts['vmax'] = clim[1]

# Check whether the colorbar should indicate clipping
el_min, el_max = element.range(vdim)
if el_min < opts['vmin'] and el_max > opts['vmax']:
opts['extend'] = 'both'
elif el_min < opts['vmin']:
self._cbar_extend = 'min'
elif el_max > opts['vmax']:
self._cbar_extend = 'max'

# Define special out-of-range colors on colormap
cmap_name = opts.pop('cmap', None)
cmap = copy.copy(plt.cm.get_cmap('gray' if cmap_name is None else cmap_name))
if 'max' in self.clipping_colors:
cmap.set_over(*util.wrap_tuple(self.clipping_colors['max']))
if 'min' in self.clipping_colors:
cmap.set_under(*util.wrap_tuple(self.clipping_colors['min']))
if 'NaN' in self.clipping_colors:
cmap.set_bad(*util.wrap_tuple(self.clipping_colors['NaN']))
opts['cmap'] = cmap



class LegendPlot(ElementPlot):

Expand Down
6 changes: 2 additions & 4 deletions holoviews/plotting/mpl/raster.py
Expand Up @@ -155,10 +155,8 @@ def get_data(self, element, ranges, style):
_, style, axis_kwargs = super(HeatMapPlot, self).get_data(element, ranges, style)
data = element.raster
data = np.ma.array(data, mask=np.logical_not(np.isfinite(data)))
cmap_name = style.pop('cmap', None)
cmap = copy.copy(plt.cm.get_cmap('gray' if cmap_name is None else cmap_name))
cmap.set_bad('w', 1.)
style['cmap'] = cmap
vdim = element.vdims[0]
self._norm_kwargs(element, ranges, style, vdim)
style['annotations'] = self._annotate_values(element)
return [data], style, axis_kwargs

Expand Down

0 comments on commit 5c7b351

Please sign in to comment.