Bokeh examples

also see Python

Basic bokeh Example


import bokeh as bk

import bokeh.layouts
import bokeh.models
import bokeh.plotting
import bokeh.io
import bokeh.palettes

from bokeh.layouts import row, column
from bokeh.models import BoxSelectTool, LassoSelectTool, Spacer
from bokeh.palettes import Spectral10



bk.io.output_notebook()


p = bk.plotting.figure(
    y_range=(0.1, 3),
    y_axis_type="log",
    x_axis_type='datetime',
    x_axis_label='Time [days]',
    plot_width=1000, plot_height=300,
    )

p.yaxis.ticker = [0.4, 1.4, 2.4]
p.yaxis.major_label_overrides = {0.4: "WI Zugnr", 1.4: "WI bSt", 2.4: "LB Zugnr"}

p.circle(x='x_values', y='y_values', legend_label='name', source=source)

p.legend.location = "top_left"
p.legend.click_policy = "hide"

bk.plotting.show(p)

Histogram

Hacked in histogram

def hist(self, data, bins=10, range=None, bottom=0, **kwargs):
    if range is None:
        range=data.min(), data.max()
    hist, edges = np.histogram(data, bins=bins, range=range)
    quads = self.quad(top=hist, bottom=bottom, left=edges[:-1], right=edges[1:], line_color="white", **kwargs)
    return quads


bk.plotting.Figure.hist = hist

Bar Plot with proper width for zooming in

p = bk.plotting.figure(
    x_axis_type='datetime',
    x_axis_label='Zeit',
    plot_width=1200,
)
 
def get_width():
    return 0.8 * (dfb.ds.max() - dfb.ds.min()).total_seconds() * 1000 / len(dfb)
 
p.vbar(
    x='ds',
    top='y',
    width=get_width(),
    source=dfb,
)
p.xaxis.formatter=bk.models.DatetimeTickFormatter(
    days="%Y-%m-%d %H:%M",
    months="%Y-%m-%d",
    hours="%Y-%m-%d %H:%M",
    minutes="%m-%d %H:%M",
)
bk.plotting.show(p)

Automatic Color Cycling

Helper Class

from itertools import cycle
 
class colors():
    cycle = None
    last_color = None
    palette = None
   
    def next(palette=bk.palettes.Category10[10]):
        if colors.cycle is None:
            colors.cycle = cycle(palette)
            colors.palette = bk.palettes.Category10[10]
        colors.last_color = next(colors.cycle)
        return colors.last_color
   
    def same():
        if colors.last_color is None:
            raise ValueError('No last color, please call .next() first')
        else:
            return colors.last_color
 
    def reset():
        colors.cycle = cycle(colors.palette)

In Usage

with a fbprophet forecast result object

colors.reset()
 
p = bk.plotting.figure(x_axis_type='datetime')
 
p.scatter(x='ds', y='y', source=dfb, legend_label='Bestellung', color=colors.next())
p.line(x='ds', y='yhat', source=forecast, legend_label='Forecast', color=colors.next())
p.varea(x='ds', y1='yhat_lower', y2='yhat_upper', source=forecast, legend_label='Uncertainty',color=colors.same(), alpha=0.3)
 
bk.plotting.show(p)