# Bokeh Tutorial

## 01. Basic Plotting

This section of the tutorial covers the bokeh.plotting interface. This interface is a "mid-level" interface, and the main idea can be described by the statement:

Starting from simple default figures (with sensible default tools, grids and axes), add markers and other shapes whose visual attributes are tied to directly data.

We will see that it is possible to customize and change all of the defaults, but having them means that it is possible to get up and running very quickly.

# Imports and Setup¶

When using the bokeh.plotting interface, there are a few common imports:

In [1]:
import numpy as np # we will use this later, so import it now

from bokeh.io import output_notebook, show
from bokeh.plotting import figure


In this case, we are in the Jupyter notebook, so we will call output_notebook() below. We only need to call this once, and all subsequent calls to show() will display inline in the notebook.

In [2]:
output_notebook()


If everything is working, you should see a Bokeh logo and a message like "BokehJS 0.13.0 successfully loaded" as the output.

# Scatter Plots¶

Bokeh can draw many types of visual shapes (called glyphs), include lines, bars, patches, hex tiles and more. One of the most common visualization tasks is to draw a scatter plot of data using small marker glyphs to represent each point.

In this section you will see how to use Bokeh's various marker glyphsto create simple scatter plots.

The basic outline is:

• create a blank figure: p = figure(...)
• call a glyph method such as p.circle on the figure
• show the figure

Execute the cell below to create a small scatter plot with circle glyphs:

In [3]:
# create a new plot with default tools, using figure
p = figure(plot_width=400, plot_height=400)

# add a circle renderer with x and y coordinates, size, color, and alpha
p.circle([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=15, line_color="navy", fill_color="orange", fill_alpha=0.5)

show(p) # show the results


In the output above, you can see the effect of the different options for line_color, fill_alpha, etc. Try changing some of these values and re-executing the cell to update the plot.

All Bokeh scatter markers accept size (measured in screen space units) as a property. Circles in particular also have radius (measured in "data" space units).

In [4]:
# EXERCISE: Try changing the example above to set a radius value instead of size


To scatter square markers instead of circles, you can use the square method on figures.

In [5]:
# create a new plot using figure
p = figure(plot_width=400, plot_height=400)

# add a square renderer with a size, color, alpha, and sizes
p.square([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=[10, 15, 20, 25, 30], color="firebrick", alpha=0.6)

show(p) # show the results


Note that in the example above, we are also specifying different sizes for each individual marker. In general, all of a glyph's properties can be "vectorized" in this fashion. Also note that we have passed color as a shorthand to set both the line and fill colors easily at the same time. This is a convenience specific to bokeh.plotting.

There are many marker types available in Bokeh, you can see details and example plots for all of them in the reference guide by clicking on entries in the list below:

In [6]:
# EXERCISE: Make a scatter plot using the "autompg" dataset

from bokeh.sampledata.autompg import autompg as df # run df.head() to inspect


# Line Plots¶

Another common visualization task is the drawing of line plots. This can be accomplished in Bokeh by calling the p.line(...) glyph method as shown below.

In [7]:
# create a new plot (with a title) using figure
p = figure(plot_width=400, plot_height=400, title="My Line Plot")

p.line([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], line_width=2)

show(p) # show the results


In addition to line_width, there are other options such as line_color or line_dash that can be set. Try setting some of the other properties of line and re-running the cell above.

### Datetime axes¶

It's often the case that timeseries data is represented by drawing lines. Let's look at an example using the "glucose" data set, which is available in a Pandas dataframe:

In [8]:
from bokeh.sampledata.glucose import data

Out[8]:
isig glucose
datetime
2010-03-24 09:51:00 22.59 258
2010-03-24 09:56:00 22.52 260
2010-03-24 10:01:00 22.23 258
2010-03-24 10:06:00 21.56 254
2010-03-24 10:11:00 20.79 246

We'd like to plot a subset of this data, and have a nice datetime axis as well. We can ask Bokeh for a datetime axis by passing x_axis_type="datetime" to the call to figure. This is shown below, as well as configuration of a some other options such as plot dimensions, axis titles, and grid line properies.

In [9]:
# reduce data size to one week
week = data.loc['2010-10-01':'2010-10-08']

p = figure(x_axis_type="datetime", title="Glocose Range", plot_height=350, plot_width=800)
p.xgrid.grid_line_color=None
p.ygrid.grid_line_alpha=0.5
p.xaxis.axis_label = 'Time'
p.yaxis.axis_label = 'Value'

p.line(week.index, week.glucose)

show(p)

In [10]:
# EXERCISE: Look at the AAPL data from bokeh.sampledata.stocks and create a line plot using it
from bokeh.sampledata.stocks import AAPL

# AAPL.keys()
# dict_keys(['date', 'open', 'high', 'low', 'close', 'volume', 'adj_close'])

dates = np.array(AAPL['date'], dtype=np.datetime64) # convert date strings to real datetimes


## Hex Tiling¶

Bokeh supports drawing low level hex tilings using axial coordinates and the hex_tile method, as described in the Hex Tiles section of the User's Guide. However, one of the most common uses of hex tilings is to visualize binning. Bokeh encapsulates this common operation in the hexbin function, whose output can be passed directly to hex_tile as seen below.

In [11]:
from bokeh.palettes import Viridis256
from bokeh.util.hex import hexbin

n = 50000
x = np.random.standard_normal(n)
y = np.random.standard_normal(n)

bins = hexbin(x, y, 0.1)

# color map the bins by hand, will see how to use linear_cmap later
color = [Viridis256[int(i)] for i in bins.counts/max(bins.counts)*255]

# match_aspect ensures neither dimension is squished, regardless of the plot size
p = figure(tools="wheel_zoom,reset", match_aspect=True, background_fill_color='#440154')
p.grid.visible = False

p.hex_tile(bins.q, bins.r, size=0.1, line_color=None, fill_color=color)

show(p)

In [12]:
# Exercise: Experiment with the size parameter to hexbin, and using different data as input


# Images¶

Another common task is to display images, which might represent heat maps, or sensor data of some sort. Bokeh provides two glyph methods for displaying images:

• image which can be used, together with a palette, to show colormapped 2d data in a plot
• image_rgba which can be used to display raw RGBA pixel data in a plot.

The first example below shows how to call image with a 2d array and a palette

In [13]:
N = 500
x = np.linspace(0, 10, N)
y = np.linspace(0, 10, N)
xx, yy = np.meshgrid(x, y)

img = np.sin(xx)*np.cos(yy)

p = figure(x_range=(0, 10), y_range=(0, 10))

# must give a vector of image data for image parameter
p.image(image=[img], x=0, y=0, dw=10, dh=10, palette="Spectral11")

show(p)