Bokeh Themes with yaml file

只谈情不闲聊 提交于 2019-12-24 20:34:15

问题


Am new to Python and Bokeh. Trying to apply a simple theme to a simple line graph using a separate yaml file. Unsure as to how to do this. If this is my cell in my Juypter notebook:

import pandas as pd
from bokeh.plotting import show, figure, output_notebook
from bokeh.palettes import Spectral
from bokeh.themes import Theme
from bokeh.document import Document

output_notebook()

x_f = [1.5, 2, 9]
y_f = [3, 3, 3.1]

p = figure(plot_width=400, plot_height=400)
p.line(x_f, y_f, line_width=3, color=Spectral[4][0])

show(p)

And this is a separate file, lp.yaml

### Default attribute of Bokeh line chart
attrs:
    Figure:
        background_fill_color: "whitesmoke"
        background_fill_alpha: 0.5

How do I override the default themes with the attributes in the .yaml file?


回答1:


the below should work in a jupyter notebook with Bokeh 12.16

from bokeh.io import show, output_notebook, curdoc
from bokeh.palettes import Spectral
from bokeh.plotting import figure
from bokeh.themes import Theme


output_notebook()


theme = Theme(json={
    'attrs': {
        'Figure': {
            'background_fill_color': '#2F2F2F',
            'border_fill_color': '#2F2F2F',
            'outline_line_color': '#444444'
            },
        'Axis': {
            'axis_line_color': "white",
            'axis_label_text_color': "white",
            'major_label_text_color': "white",
            'major_tick_line_color': "white",
            'minor_tick_line_color': "white",
            'minor_tick_line_color': "white"
            },
        'Grid': {
            'grid_line_dash': [6, 4],
            'grid_line_alpha': .3
            },
        'Circle': {
            'fill_color': 'lightblue',
            'size': 10,
            },
        'Title': {
            'text_color': "white"
            }
        }
    })



x_f = [1.5, 2, 9]
y_f = [3, 3, 3.1]

p = figure(plot_width=400, plot_height=400)
p.line(x_f, y_f, line_width=3, color=Spectral[4][0])

doc = curdoc()
doc.theme = theme
doc.add_root(p)
show(p)



回答2:


See file structure below. Run as (tested with Bokeh v0.12.6):

bokeh serve --show myapp

File structure:

myapp
   |
   +---main.py
   +---theme.yaml
   +---templates
        +---index.html

main.py

from bokeh.plotting import curdoc, show, figure, output_notebook
from bokeh.palettes import Spectral

output_notebook()

x_f = [1.5, 2, 9]
y_f = [3, 3, 3.1]

p = figure(plot_width = 400, plot_height = 400)
p.line(x_f, y_f, line_width = 3, color = Spectral[4][0])
curdoc().add_root(p)

theme.yaml

attrs:
    Figure:
        background_fill_color: "whitesmoke"
        background_fill_alpha: 0.5

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    {{ bokeh_css }}
    {{ bokeh_js }}
  </head>
  <body>
    {{ plot_div|indent(8) }}
    {{ plot_script|indent(8) }}
  </body>
</html>


来源:https://stackoverflow.com/questions/48756660/bokeh-themes-with-yaml-file

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