Bokeh


In [ ]:
import bokeh
from bokeh.plotting import figure, output_notebook, show

Plot lines


In [ ]:
# prepare some data
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]

output_notebook()

# create a new plot with a title and axis labels
p = figure(title="simple line example",
           x_axis_label='x',
           y_axis_label='y')

# add a line renderer with legend and line thickness
p.line(x, y, legend="Temp.", line_width=2)

# show the results
show(p)

Scatter plot

Circles


In [ ]:
output_notebook()

p = figure(plot_width=400, plot_height=400)

# add a circle renderer with a size, color, and alpha
p.circle([1, 2, 3, 4, 5],
         [6, 7, 2, 4, 5],
         size=20,
         color="navy",
         alpha=0.5)

# show the results
show(p)

Squares


In [ ]:
output_notebook()

p = figure(plot_width=400, plot_height=400)

# add a square renderer with a size, color, and alpha
p.square([1, 2, 3, 4, 5],
         [6, 7, 2, 4, 5],
         size=20,
         color="olive",
         alpha=0.5)

# show the results
show(p)

Hex Tiles

Bokeh can plot hexagonal tiles, which are often used for showing binned aggregations. The hex_tile() method takes a size parameter to define the size of the hex grid, and axial coordinates to specify which tiles are present.


In [ ]:
import numpy as np

from bokeh.io import output_notebook, show
from bokeh.plotting import figure
from bokeh.util.hex import axial_to_cartesian

output_notebook()

q = np.array([0,  0, 0, -1, -1,  1, 1])
r = np.array([0, -1, 1,  0,  1, -1, 0])

p = figure(plot_width=400, plot_height=400, toolbar_location=None)
p.grid.visible = False

p.hex_tile(q, r, size=1, fill_color=["firebrick"]*3 + ["olive"]*2 + ["navy"]*2,
           line_color="white", alpha=0.5)

x, y = axial_to_cartesian(q, r, 1, "pointytop")

p.text(x, y, text=["(%d, %d)" % (q,r) for (q, r) in zip(q, r)],
       text_baseline="middle", text_align="center")

show(p)

Palettes and color mappers

Palettes


In [ ]:
bokeh.palettes.Category10_3

In [ ]:
bokeh.palettes.Category10_4

In [ ]:
bokeh.palettes.viridis(10)

In [ ]:
bokeh.palettes.Viridis256

Color Mappers


In [ ]:
output_notebook()

p = figure(plot_width=400, plot_height=400)

x = np.array([1, 2, 3, 4, 5])
y = np.array([6, 7, 2, 4, 5])

color_mapper = bokeh.models.mappers.LinearColorMapper(palette=bokeh.palettes.Viridis256,
                                                      low=y.min(),
                                                      high=y.max())

# add a circle renderer with a size, color, and alpha
p.circle(x,
         y,
         size=20,
         color="navy",
         fill_color=bokeh.transform.transform('y', color_mapper),
         alpha=0.5)

# show the results
show(p)

Export to PNG or SVG

Tooltips


In [ ]:
from bokeh.plotting import figure, output_file, show, ColumnDataSource
from bokeh.models import HoverTool
from bokeh.io import output_notebook

output_notebook()

source = ColumnDataSource(
             data=dict(
                 x=[1, 2, 3, 4, 5],
                 y=[2, 5, 8, 2, 7],
                 desc=['A', 'b', 'C', 'd', 'E'],
             )
         )

hover = HoverTool(
            tooltips=[
                ("index", "$index"),
                ("(x,y)", "($x, $y)"),
                ("desc", "@desc"),
            ]
        )

fig = figure(plot_width=300, plot_height=300, #tools=[hover],
             title="Mouse over the dots")
fig.add_tools(hover)

fig.circle('x', 'y', size=10, source=source)

show(fig)

Update


In [ ]:
from bokeh.io import push_notebook, output_notebook, show
from bokeh.plotting import figure

In [ ]:
output_notebook()

p = figure(plot_width=400, plot_height=400)

# add a square renderer with a size, color, and alpha
s = p.square([1, 2, 3, 4, 5],
             [6, 7, 2, 4, 5],
             size=20,
             color="olive",
             alpha=0.5)

# show the results
h = show(p, notebook_handle=True)

In [ ]:
h

In [ ]:
s.glyph.fill_color = "navy"
push_notebook(handle=h)

Animation


In [ ]:
from bokeh.io import push_notebook, output_notebook, show
from bokeh.plotting import figure

In [ ]:
import time

In [ ]:
import numpy as np

In [ ]:
# prepare some data
x = np.linspace(-2. * np.pi, 2. * np.pi, 1000)
y = np.sin(x)

output_notebook()

# create a new plot with a title and axis labels
p = figure(title="ipywidgets test",
           x_axis_label='x',
           y_axis_label='y')

# add a line renderer with legend and line thickness
l = p.line(x, y, legend="Sin(x)", line_width=2)

# show the results
h = show(p, notebook_handle=True)                  # <-- !!!

In [ ]:
for t in range(200):
    l.data_source.data['y'] = np.sin(x - 0.1 * t)
    push_notebook(handle=h)
    time.sleep(0.01)

Interactive plots


In [ ]:
from bokeh.io import push_notebook, output_notebook, show
from bokeh.plotting import figure

In [ ]:
import ipywidgets
from ipywidgets import interact

In [ ]:
import numpy as np

In [ ]:
# prepare some data
x = np.linspace(-2. * np.pi, 2. * np.pi, 1000)
y = np.sin(x)

output_notebook()

# create a new plot with a title and axis labels
p = figure(title="ipywidgets test",
           x_axis_label='x',
           y_axis_label='y')

# add a line renderer with legend and line thickness
l = p.line(x, y, legend="Sin(x)", line_width=2)

# show the results
h = show(p, notebook_handle=True)

In [ ]:
@interact(t=(0, 100, 1))
def update_plot(t):
    l.data_source.data['y'] = np.sin(x - 0.1 * t)
    
    push_notebook(handle=h)