import bokeh.plotting as bk
bk.output_notebook() # show plots in the notebook
p = bk.figure(plot_width=300, plot_height=300, title='The first Bokeh plot')
x = [1, 2, 3, 4, 5]
y = [4, 2, 1, 2, 3]
p.circle(x, y, size=20, color='red')
bk.show(p)
p = bk.figure(plot_width=300, plot_height=300, title='The first Bokeh plot')
x = [1, 2, 3, 4, 5]
y = [4, 2, 1, 2, 3]
p.square(x, y, size=20, color='green')
bk.show(p)
p = bk.figure(plot_width=300, plot_height=300, title='The first Bokeh plot')
x = [1, 2, 3, 4, 5]
y = [4, 2, 1, 2, 3]
p.line(x, y, line_width=2, color='blue')
bk.show(p)
Specifying colors by HTML color names:
p = bk.figure(plot_width=300, plot_height=300)
x = [0, 0, 0]
y = [0, 0, 0]
p.circle(x, y, size=[100, 60, 20], color=['FireBrick', 'ForestGreen', 'Gold'])
bk.show(p)
Specifying colors by RGB coordinates (RGB coordinates are integers in the range 0-255):
p = bk.figure(plot_width=300, plot_height=300)
x = [0, 1, 2]
y = [0, 1, 2]
p.circle(x, y, size=[100, 60, 20], color=(255, 0, 0))
bk.show(p)
This does not work:
p = bk.figure(plot_width=300, plot_height=300)
x = [0, 1, 2]
y = [0, 1, 2]
p.circle(x, y, size=[100, 60, 20], color=[(255, 0, 0), (0, 255, 0), (0,0,255)])
bk.show(p)
This works:
p = bk.figure(plot_width=300, plot_height=300)
x = [0, 1, 2]
y = [0, 1, 2]
p.circle(x, y, size=[100, 60, 20], color=['rgb(255, 0, 0)', 'rgb(0, 255, 0)', 'rgb(0,0,255)'])
bk.show(p)
Specifying colors using palettes (i.e. color maps):
import bokeh.palettes
from bokeh.palettes import Inferno10
Inferno10[3]
p = bk.figure(plot_width=300, plot_height=300)
x = [0, 0, 0]
y = [0, 0, 0]
p.circle(x, y, size=[100, 60, 20], color=[Inferno10[0], Inferno10[5], Inferno10[9]])
bk.show(p)
Transparency:
import numpy as np
p = bk.figure(plot_width=400, plot_height=400)
x = np.random.rand(20)
y = np.random.rand(20)
r = np.random.rand(20)*0.25
p.circle(x,y, radius=r, alpha=0.2)
bk.show(p)
import numpy as np
p = bk.figure(plot_width=400, plot_height=400)
p.circle(0,0, radius=1, alpha=0.2)
bk.show(p)
p = bk.figure(plot_width=400, plot_height=400, match_aspect=True)
p.ellipse(0, 0, width=2, height=2, color="#386CB0", fill_color=None, line_width=2)
bk.show(p)
p = bk.figure(plot_width=600, plot_height=250)
x = np.linspace(-6, 6, 200)
p.line(x, np.sin(x), line_width=2, color='red')
p.line(x, np.cos(x), line_width=2, color='blue')
bk.show(p)
Setting up axes ranges:
p = bk.figure(plot_width=600, plot_height=250, x_range=(-2, 2), y_range=(-2, 2))
x = np.linspace(-6, 6, 200)
p.line(x, np.sin(x), line_width=2, color='red')
p.line(x, np.cos(x), line_width=2, color='blue')
bk.show(p)
p = bk.figure(plot_width=600, plot_height=250)
x = np.linspace(-6, 6, 200)
p.line(x, np.sin(x), line_width=2, color='red', legend='y=sin(x)')
p.line(x, np.cos(x), line_width=2, color='blue', legend='y=cos(x)')
p.circle(x[::5], np.sin(x[::5]), size=7, fill_color='white', line_color='red', legend='y=sin(x)')
bk.show(p)
Subplots:
p1 = bk.figure(plot_width=250, plot_height=250)
p1.line(x, np.sin(x), color='red', line_width=2)
p2 = bk.figure(plot_width=250, plot_height=250)
p2.line(x, np.cos(x), color='green', line_width=2)
p3 = bk.figure(plot_width=250, plot_height=250)
p3.line(x, x**2, color='blue', line_width=2)
p4 = bk.figure(plot_width=250, plot_height=250, title='y=x')
p4.line(x, x, color='gray', line_width=2)
from bokeh.layouts import gridplot
bk.show(gridplot([p1, p2, p3, p4], ncols =3))
Plots with shared axes ranges:
t = np.linspace(0, 1, 10)
acc = t
vel = (1/2)*t**2
pos = (1/6)*t**3
a = bk.figure(plot_width=250, plot_height=250, title='Acceleration')
a.line(x=t, y=acc, color='red')
a.circle(x=t, y=acc, color='blue', size=8)
v = bk.figure(plot_width=250, plot_height=250, title='Velocity', x_range=a.x_range)
v.line(x=t, y=vel, color='red')
v.circle(x=t, y=vel, color='blue', size=8)
p = bk.figure(plot_width=250, plot_height=250, title='Position', x_range = a.x_range)
p.line(x=t, y=pos, color='red')
p.circle(x=t, y=pos, color='blue', size=8)
bk.show(gridplot([a, v, p], ncols=3))
from bokeh.models import ColumnDataSource
t = np.linspace(0, 1, 10)
coords = {'time': t,
'acceleration': t,
'velocity': (1/2)*t**2,
'position': (1/6)*t**3,
'mycolors': Inferno10
}
data = ColumnDataSource(data=coords)
a = bk.figure(plot_width=250, plot_height=250, title='Acceleration')
a.line(x='time', y='acceleration', color='red', source=data)
a.circle(x='time', y='acceleration', color='mycolors', size=8, source=data)
v = bk.figure(plot_width=250, plot_height=250, title='Velocity', x_range=a.x_range)
v.line(x='time', y='velocity', color='red', source=data)
v.circle(x='time', y='velocity', color='blue', size=8, source=data)
p = bk.figure(plot_width=250, plot_height=250, title='Position', x_range = a.x_range)
p.line(x='time', y='position', color='red',source=data)
p.circle(x='time', y='position', color='blue', size=8, source=data)
bk.show(gridplot([a, v, p], ncols=3))
from bokeh.models import ColumnDataSource
t = np.linspace(0, 1, 10)
coords = {'time': t,
'acceleration': t,
'velocity': (1/2)*t**2,
'position': (1/6)*t**3,
'mycolors': Inferno10
}
data = ColumnDataSource(data=coords)
TOOLS = ['crosshair', 'wheel_zoom', 'box_zoom', 'reset', 'box_select', 'lasso_select', 'hover', 'tap']
a = bk.figure(plot_width=250, plot_height=250, title='Acceleration', tools=TOOLS)
a.circle(x='time', y='acceleration', color='mycolors', size=8, source=data, hover_color='red')
v = bk.figure(plot_width=250, plot_height=250, title='Velocity', x_range=a.x_range, tools=TOOLS)
v.circle(x='time', y='velocity', color='blue', size=8, source=data, hover_color='red')
p = bk.figure(plot_width=250, plot_height=250, title='Position', x_range = a.x_range, tools=TOOLS)
p.circle(x='time', y='position', color='blue', size=8, source=data, hover_color='red')
bk.show(gridplot([a, v, p], ncols=3))
from bokeh.models import HoverTool
coords = {'x_coords': [1, 2, 3, 4],
'y_coords': [2, 1, 0, 1],
'description': ['Point A', 'Point B', 'Point C', 'Point D']
}
data = ColumnDataSource(data=coords)
myhover = HoverTool(tooltips= [('x position', '@x_coords'), ('y position', '@y_coords'), ('name', '@description')])
TOOLS = ['crosshair', 'wheel_zoom', 'box_zoom', 'reset', 'box_select', 'lasso_select', myhover, 'tap']
p = bk.figure(plot_width=400, plot_height=400, tools=TOOLS)
p.circle(x='x_coords', y='y_coords', size=15, source = data, color='green', hover_color='red')
bk.show(p)
Saving a plot to an HTML file:
bk.reset_output()
bk.output_file('myplot.html')
from bokeh.models import HoverTool
coords = {'x_coords': [1, 2, 3, 4],
'y_coords': [2, 1, 0, 1],
'description': ['Point A', 'Point B', 'Point C', 'Point D']
}
data = ColumnDataSource(data=coords)
myhover = HoverTool(tooltips= [('x position', '@x_coords'), ('y position', '@y_coords'), ('name', '@description')])
TOOLS = ['crosshair', 'wheel_zoom', 'box_zoom', 'reset', 'box_select', 'lasso_select', myhover, 'tap']
p = bk.figure(plot_width=400, plot_height=400, tools=TOOLS)
p.circle(x='x_coords', y='y_coords', size=15, source = data, color='green', hover_color='red')
bk.show(p)