How to set the default style in Bokeh?

梦想与她 提交于 2019-12-01 03:13:43

问题


I'm writing a report whose plots are all rendered with Matplotlib. I've adjusted Matplotlib's default to ensure that all plots have the same style.

However, I need to use Bokeh since it provides support for rendering legends for Datashader - a library being developed by the folks at Bokeh.

My issue is that the default Bokeh style is very different from my custom style. Rather than changing every single attribute in my Bokeh plot would it be possible to have Bokeh read from a style sheet in a similar way as Matplotlib does with plt.use.style(['ggplot'])?


回答1:


As of Bokeh 0.12.4 there are still open issues (features to develop as well as a few bugs, and more documentation support) around theming in Bokeh. What is currently supported is type-based theming using a Theme object that can be set on the current document.

The Theme object takes a JSON block, of the general form:

 { 
   'attrs: {
       'SomeTypeName': { 'foo_property': default_foo },
       'OtherTypeName': { 'bar_property': default_bar }
   }
 }

Or for a concrete example:

from bokeh.io import curdoc
from bokeh.themes import Theme

curdoc().theme = Theme(json={'attrs': {

    # apply defaults to Figure properties
    'Figure': {
        'toolbar_location': None,
        'outline_line_color': None,
        'min_border_right': 10,
    },

    # apply defaults to Axis properties
    'Axis': {
        'major_tick_in': None,
        'minor_tick_out': None,
        'minor_tick_in': None,
        'axis_line_color': '#CAC6B6',
        'major_tick_line_color': '#CAC6B6',
    },

     # apply defaults to Legend properties
    'Legend': {
        'background_fill_alpha': 0.8,
    }
}})

This JSON could also be read from a file using standard Python JSON tools.

If this also happens to be in the context of a (directory style) Bokeh server application, you can also provide the theme as a theme.yaml file in the same directory as your main.py. See, e.g., the Gapminder example.



来源:https://stackoverflow.com/questions/41987539/how-to-set-the-default-style-in-bokeh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!