Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the default style in Bokeh?

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

like image 233
gzagatti Avatar asked Oct 18 '22 17:10

gzagatti


1 Answers

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.

like image 159
bigreddot Avatar answered Oct 20 '22 11:10

bigreddot