0

I would like to configure my default options in HoloViews to match those I have been using in my Bokeh plots, but while I can find many equivalents in the HoloViews documentation, I can't figure out what the equivalents are for others.

For example, I have started with the ones I can find in the HoloViews documentation using

opts.defaults(
    opts.Scatter(fill_color='black', line_color='gray', fill_alpha=0.1, line_alpha=1.0, 
                 hover_fill_color='yellow', hover_line_color='black', hover_fill_alpha=1.0, hover_line_alpha=1.0,
                 nonselection_fill_color='gray', nonselection_line_color=None, nonselection_alpha=0.2, 
                 selection_fill_color='black', selection_line_color='white', selection_alpha=1.0, 
                 size=6, line_width=1),
    opts.Histogram(fill_color='gray', fill_alpha=0.9, line_width=1, line_color='gray'),
    opts.Text(text_color='green')
)

but for many others, especially relating to fonts and control over tick length and colors, I can't find equivalents. In Bokeh I can set these options that I'm interested in for for a given plot with something like

p = figure(...)
# ...

p.xaxis.axis_label = x_label
p.yaxis.axis_label = y_label
p.xaxis.axis_label_text_font = FONT
p.axis.axis_label_text_color = "gray"
p.axis.axis_label_text_font_style = "normal"

p.axis.axis_line_color = "gray"
p.axis.major_label_text_color = "gray"

p.axis.major_tick_line_color = "gray"
p.axis.minor_tick_line_color = "gray"

p.axis.minor_tick_in = 0
p.axis.major_tick_in = 0
p.axis.major_tick_out = 5  
p.axis.minor_tick_out = 2

p.grid.grid_line_alpha = 0.5
p.grid.grid_line_dash = [6, 4]

p.title.text_color = "gray"
p.title.text_font = FONT
p.title.text_font_style = "normal"

p.title.align = "center"

p.toolbar.autohide = True

but I'm unsure how to set these in HoloViews using opts.defaults.

How do I set these options using HoloViews? Is there perhaps some general mechanism to "pass" these Bokeh options to HoloViews in opts.defaults?

bad_coder
  • 11,289
  • 20
  • 44
  • 72
orome
  • 45,163
  • 57
  • 202
  • 418

1 Answers1

2

According to documentation you should be able to get a reference to the Bokeh Figure object and set at least some attributes using plot hooks:

import numpy as np
import holoviews as hv

hv.extension('bokeh')

def hook(plot, element):
    print('plot.state:   ', plot.state)
    print('plot.handles: ', sorted(plot.handles.keys()))
    print(plot.handles['xaxis'])
    print(plot.state.grid)
    print(plot.state.title)

    plot.state.title.align = "center"
    plot.state.title.text = 'Scatter Plot'

    plot.handles['xaxis'].minor_tick_in = 0
    plot.handles['xaxis'].major_tick_in = 0
    plot.handles['xaxis'].major_tick_out = 5
    plot.handles['xaxis'].minor_tick_out = 2
    plot.handles['xaxis'].axis_label = 'X-AXIS-GREEN'
    plot.handles['yaxis'].axis_label = 'Y-AXIS-RED'
    plot.handles['xaxis'].axis_label_text_color = 'green'
    plot.handles['yaxis'].axis_label_text_color = 'red'

    plot.handles['yaxis'].axis_label_text_color = 'red'

scatter = hv.Points(np.random.randn(1000, 2))
scatter = scatter.opts(hooks = [hook])

renderer = hv.renderer('bokeh')
renderer.save(scatter, 'testHV')

Result:

enter image description here

Tony
  • 7,767
  • 2
  • 22
  • 51
  • That's pretty low level. So just to confirm: there's no property for setting styles on axes, other than the `hook` mechanism? What about setting the font face? – orome Apr 09 '19 at 16:54
  • HoloViews abstracts plot creation but this has its price. Only accessing the back-end directly provides access to its (low-level) attributes. You can set the font style the same way as above. I don't know about any other options. – Tony Apr 09 '19 at 18:16
  • Cool. As long is it's the idiom, it makes sense. – orome Apr 09 '19 at 18:17
  • Any idea how to set up the corresponding default options for Matplotlib? The obvious approach of having `opts.defaults(..., backend='matplotlib')` only partly works (some options are ignored, others cause warning about being wrong for the Bokeh backend). Perhaps a separate question, but perhaps there's a quick answer that would avoid a parallel question. – orome Apr 09 '19 at 21:33
  • More on this as it applies to multiple backends, and consistent appearance across backends [here](https://stackoverflow.com/q/55614695/656912). – orome Apr 10 '19 at 14:12