Skip to content

Commit

Permalink
Implement legend_labels in mpl
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Jun 24, 2022
1 parent bbed3f3 commit 550f16b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
12 changes: 10 additions & 2 deletions holoviews/plotting/mpl/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,9 @@ def update_frame(self, key, ranges=None, element=None):
style = self.lookup_options(element, 'style')
self.style = style.max_cycles(max_cycles) if max_cycles else style

labels = getattr(self, 'legend_labels', {})
label = element.label if self.show_legend else ''
style = dict(label=label, zorder=self.zorder, **self.style[self.cyclic_index])
style = dict(label=labels.get(label, label), zorder=self.zorder, **self.style[self.cyclic_index])
axis_kwargs = self.update_handles(key, axis, element, ranges, style)
self._finalize_axis(key, element=element, ranges=ranges,
**(axis_kwargs if axis_kwargs else {}))
Expand Down Expand Up @@ -615,6 +616,8 @@ def _apply_transforms(self, element, ranges, style):
else:
factors = util.unique_array(val)
val = util.search_indices(val, factors)
labels = getattr(self, 'legend_labels', {})
factors = [labels.get(f, f) for f in factors]
new_style['cat_legend'] = {
'title': v.dimension, 'prop': 'c', 'factors': factors
}
Expand Down Expand Up @@ -1010,6 +1013,9 @@ class LegendPlot(ElementPlot):
legend_cols = param.Integer(default=None, doc="""
Number of legend columns in the legend.""")

legend_labels = param.Dict(default={}, doc="""
A mapping that allows overriding legend labels.""")

legend_position = param.ObjectSelector(objects=['inner', 'right',
'bottom', 'top',
'left', 'best',
Expand Down Expand Up @@ -1049,6 +1055,7 @@ def _legend_opts(self):
legend_opts.update(**dict(leg_spec, **self._fontsize('legend')))
return legend_opts


class OverlayPlot(LegendPlot, GenericOverlayPlot):
"""
OverlayPlot supports compositors processing of Overlays across maps.
Expand Down Expand Up @@ -1087,6 +1094,7 @@ def _adjust_legend(self, overlay, axis):
for key, subplot in self.subplots.items():
element = overlay.data.get(key, False)
if not subplot.show_legend or not element: continue
labels = subplot.legend_labels
title = ', '.join([d.name for d in dimensions])
handle = subplot.traverse(lambda p: p.handles['artist'],
[lambda p: 'artist' in p.handles])
Expand All @@ -1101,7 +1109,7 @@ def _adjust_legend(self, overlay, axis):
if isinstance(subplot, OverlayPlot):
legend_data += subplot.handles.get('legend_data', {}).items()
elif element.label and handle:
legend_data.append((handle, element.label))
legend_data.append((handle, labels.get(element.label, element.label)))
all_handles, all_labels = list(zip(*legend_data)) if legend_data else ([], [])
data = OrderedDict()
used_labels = []
Expand Down
1 change: 1 addition & 0 deletions holoviews/plotting/mpl/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ class RasterGridPlot(GridPlot, OverlayPlot):
invert_zaxis = param.Parameter(precedence=-1)
labelled = param.Parameter(precedence=-1)
legend_cols = param.Parameter(precedence=-1)
legend_labels = param.Parameter(precedence=-1)
legend_position = param.Parameter(precedence=-1)
legend_opts = param.Parameter(precedence=-1)
legend_limit = param.Parameter(precedence=-1)
Expand Down

0 comments on commit 550f16b

Please sign in to comment.