如何在HoloViews中设置Bokeh刻度和字体选项?

时间:2019-04-05 19:56:34

标签: bokeh options holoviews

我想在HoloViews中配置我的默认选项,以匹配我在Bokeh图中使用的默认选项,但是尽管我可以在HoloViews文档中找到许多等效项,但无法弄清楚其他等效项。

例如,我从使用HoloViews文档中可以找到的内容开始

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')
)

但是对于许多其他字体,尤其是字体和对刻度线长度和颜色的控制,我找不到对应的字体。在Bokeh中,我可以使用诸如此类的给定情节设置这些感兴趣的选项

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

但是我不确定如何使用opts.defaults在HoloViews中进行设置。

如何使用HoloViews设置这些选项?可能有某种通用机制将这些Bokeh选项“传递”给opts.defaults中的HoloViews吗?

1 个答案:

答案 0 :(得分:1)

根据documentation,您应该能够获取对Bokeh Figure对象的引用,并使用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')

结果:

enter image description here