0
votes

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?

1

1 Answers

2
votes

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