Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to mute legend entries by default #2831

Merged
merged 2 commits into from
Jun 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions holoviews/plotting/bokeh/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ def initialize_plot(self, ranges=None, plot=None, plots=None, source=None):
else:
self.handles['xaxis'] = plot.xaxis[0]
self.handles['x_range'] = plot.x_range
self.handles['y_axis'] = plot.yaxis[0]
self.handles['yaxis'] = plot.yaxis[0]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor unrelated fix that snuck in.

self.handles['y_range'] = plot.y_range
self.handles['plot'] = plot

Expand Down Expand Up @@ -1181,6 +1181,9 @@ class LegendPlot(ElementPlot):
options. The predefined options may be customized in the
legend_specs class attribute.""")

legend_muted = param.Boolean(default=False, doc="""
Controls whether the legend entries are muted by default.""")

legend_offset = param.NumericTuple(default=(0, 0), doc="""
If legend is placed outside the axis, this determines the
(width, height) offset in pixels from the original position.""")
Expand Down Expand Up @@ -1217,6 +1220,13 @@ def _process_legend(self, plot=None):
else:
legend.location = pos

# Apply muting
for leg in plot.legend:
for item in leg.items:
for r in item.renderers:
r.muted = self.legend_muted



class OverlayPlot(GenericOverlayPlot, LegendPlot):

Expand All @@ -1235,7 +1245,7 @@ class OverlayPlot(GenericOverlayPlot, LegendPlot):
'yticks', 'xrotation', 'yrotation', 'lod',
'border', 'invert_xaxis', 'invert_yaxis', 'sizing_mode',
'title_format', 'legend_position', 'legend_offset',
'legend_cols', 'gridstyle']
'legend_cols', 'gridstyle', 'legend_muted']

def _process_legend(self):
plot = self.handles['plot']
Expand Down Expand Up @@ -1312,6 +1322,12 @@ def _process_legend(self):
legend.location = self.legend_offset
plot.add_layout(legend, pos)

# Apply muting
for leg in plot.legend:
for item in leg.items:
for r in item.renderers:
r.muted = self.legend_muted


def _init_tools(self, element, callbacks=[]):
"""
Expand Down
15 changes: 14 additions & 1 deletion tests/plotting/bokeh/testelementplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import numpy as np

from holoviews.core import Dimension, DynamicMap
from holoviews.core import Dimension, DynamicMap, NdOverlay
from holoviews.element import Curve, Image, Scatter, Labels
from holoviews.streams import Stream
from holoviews.plotting.util import process_cmap
Expand Down Expand Up @@ -193,3 +193,16 @@ def test_overlay_gridstyle_applies(self):
plot = bokeh_renderer.get_plot(overlay)
self.assertEqual(plot.state.xgrid[0].grid_line_color, 'blue')
self.assertEqual(plot.state.xgrid[0].grid_line_width, 2)

def test_ndoverlay_legend_muted(self):
overlay = NdOverlay({i: Curve(np.random.randn(10).cumsum()) for i in range(5)}).options(legend_muted=True)
plot = bokeh_renderer.get_plot(overlay)
for sp in plot.subplots.values():
self.assertTrue(sp.handles['glyph_renderer'].muted)

def test_overlay_legend_muted(self):
overlay = (Curve(np.random.randn(10).cumsum(), label='A') *
Curve(np.random.randn(10).cumsum(), label='B')).options(legend_muted=True)
plot = bokeh_renderer.get_plot(overlay)
for sp in plot.subplots.values():
self.assertTrue(sp.handles['glyph_renderer'].muted)