Skip to content

Commit

Permalink
Unify hooks across backends (#4157)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Jan 6, 2020
1 parent ac8902c commit bf9f2a7
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 19 deletions.
8 changes: 0 additions & 8 deletions holoviews/plotting/bokeh/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,6 @@ class ElementPlot(BokehPlot, GenericElementPlot):

responsive = param.ObjectSelector(default=False, objects=[False, True, 'width', 'height'])

finalize_hooks = param.HookList(default=[], doc="""
Deprecated; use hooks options instead.""")

hooks = param.HookList(default=[], doc="""
Optional list of hooks called when finalizing a plot. The
hook is passed the plot object and the displayed element, and
other plotting handles can be accessed via plot.handles.""")

fontsize = param.Parameter(default={'title': '12pt'}, allow_None=True, doc="""
Specifies various fontsizes of the displayed text.
Expand Down
8 changes: 0 additions & 8 deletions holoviews/plotting/mpl/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,6 @@ class MPLPlot(DimensionedPlot):
plot object and the displayed object; other plotting handles
can be accessed via plot.handles.""")

finalize_hooks = param.HookList(default=[], doc="""
Deprecated; use hooks options instead.""")

hooks = param.HookList(default=[], doc="""
Optional list of hooks called when finalizing a plot. The
hook is passed the plot object and the displayed element, and
other plotting handles can be accessed via plot.handles.""")

sublabel_format = param.String(default=None, allow_None=True, doc="""
Allows labeling the subaxes in each plot with various formatters
including {Alpha}, {alpha}, {numeric} and {roman}.""")
Expand Down
2 changes: 2 additions & 0 deletions holoviews/plotting/mpl/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ class RasterGridPlot(GridPlot, OverlayPlot):
bgcolor = param.Parameter(precedence=-1)
data_aspect = param.Parameter(precedence=-1)
default_span = param.Parameter(precedence=-1)
hooks = param.Parameter(precedence=-1)
finalize_hooks = param.Parameter(precedence=-1)
invert_axes = param.Parameter(precedence=-1)
invert_xaxis = param.Parameter(precedence=-1)
invert_yaxis = param.Parameter(precedence=-1)
Expand Down
12 changes: 12 additions & 0 deletions holoviews/plotting/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,11 @@ class GenericElementPlot(DimensionedPlot):
0 a default_span of 2.0 will result in axis ranges spanning
from -1 to 1.""")

hooks = param.HookList(default=[], doc="""
Optional list of hooks called when finalizing a plot. The
hook is passed the plot object and the displayed element, and
other plotting handles can be accessed via plot.handles.""")

invert_axes = param.Boolean(default=False, doc="""
Whether to invert the x- and y-axis""")

Expand All @@ -889,6 +894,9 @@ class GenericElementPlot(DimensionedPlot):
invert_yaxis = param.Boolean(default=False, doc="""
Whether to invert the plot y-axis.""")

finalize_hooks = param.HookList(default=[], doc="""
Deprecated; use hooks options instead.""")

logx = param.Boolean(default=False, doc="""
Whether the x-axis of the plot will be a log axis.""")

Expand Down Expand Up @@ -1093,6 +1101,10 @@ def _execute_hooks(self, element):
self.param.warning(
"Supply either hooks or finalize_hooks not both, "
"using hooks and ignoring finalize_hooks.")
elif self.finalize_hooks:
self.param.warning(
"The finalize_hooks option is deprecated, use the "
"hooks option instead.")
hooks = self.hooks or self.finalize_hooks
for hook in hooks:
try:
Expand Down
6 changes: 3 additions & 3 deletions holoviews/plotting/plotly/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,15 @@ def generate_plot(self, key, ranges, element=None):
for k in ['images', 'shapes', 'annotations']:
layout.setdefault(k, [])
layout[k].extend(components.get(k, []))

self.handles['layout'] = layout

# Create figure and return it
self.drawn = True
fig = dict(data=components['traces'], layout=layout)
self.handles['fig'] = fig

self._execute_hooks(element)
self.drawn = True

self.handles['fig'] = fig
return fig


Expand Down
7 changes: 7 additions & 0 deletions holoviews/tests/plotting/bokeh/testelementplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ def test_element_title_format(self):
title = 'Label: the_label, group: the_group, dims: , type: Scatter'
self.assertEqual(render(e).title.text, title)

def test_element_hooks(self):
def hook(plot, element):
plot.handles['plot'].title.text = 'Called'
curve = Curve(range(10), label='Not Called').opts(hooks=[hook])
plot = bokeh_renderer.get_plot(curve)
self.assertEqual(plot.state.title.text, 'Called')

def test_element_xformatter_string(self):
curve = Curve(range(10)).options(xformatter='%d')
plot = bokeh_renderer.get_plot(curve)
Expand Down
7 changes: 7 additions & 0 deletions holoviews/tests/plotting/matplotlib/testelementplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ def test_stream_cleanup(self):
plot.cleanup()
self.assertFalse(bool(stream._subscribers))

def test_element_hooks(self):
def hook(plot, element):
plot.handles['title'].set_text('Called')
curve = Curve(range(10), label='Not Called').opts(hooks=[hook])
plot = mpl_renderer.get_plot(curve)
self.assertEqual(plot.handles['title'].get_text(), 'Called')

def test_element_xlabel(self):
element = Curve(range(10)).options(xlabel='custom x-label')
axes = mpl_renderer.get_plot(element).handles['axis']
Expand Down
7 changes: 7 additions & 0 deletions holoviews/tests/plotting/plotly/testelementplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ def history_callback(x, history=deque(maxlen=10)):
self.assertEqual(state['data'][0]['x'], np.arange(10))
self.assertEqual(state['data'][0]['y'], np.arange(10, 20))

def test_element_hooks(self):
def hook(plot, element):
plot.state['layout']['title'] = 'Called'
curve = Curve(range(10), label='Not Called').opts(hooks=[hook])
plot = plotly_renderer.get_plot(curve)
self.assertEqual(plot.state['layout']['title'], 'Called')

### Axis labelling ###

def test_element_plot_xlabel(self):
Expand Down

0 comments on commit bf9f2a7

Please sign in to comment.