# Imports and Setup¶

First, let's make the standard imports

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

In [2]:
output_notebook()


In [3]:
import bokeh.sampledata

Using data directory: /Users/bryan/.bokeh/data
Skipping 'CGM.csv' (checksum match)
Skipping 'US_Counties.zip' (checksum match)
Skipping 'us_cities.json' (checksum match)
Skipping 'unemployment09.csv' (checksum match)
Skipping 'AAPL.csv' (checksum match)
Skipping 'FB.csv' (checksum match)
Skipping 'GOOG.csv' (checksum match)
Skipping 'IBM.csv' (checksum match)
Skipping 'MSFT.csv' (checksum match)
Skipping 'WPP2012_SA_DB03_POPULATION_QUINQUENNIAL.zip' (checksum match)
Skipping 'gapminder_fertility.csv' (checksum match)
Skipping 'gapminder_population.csv' (checksum match)
Skipping 'gapminder_life_expectancy.csv' (checksum match)
Skipping 'gapminder_regions.csv' (checksum match)
Skipping 'world_cities.zip' (checksum match)
Skipping 'airports.json' (checksum match)
Skipping 'movies.db.zip' (checksum match)
Skipping 'airports.csv' (checksum match)
Skipping 'routes.csv' (checksum match)


# Overview¶

We've seen how Bokeh can work well with Python lists, NumPy arrays, Pandas series, etc. At lower levels, these inputs are converted to a Bokeh ColumnDataSource. This data type is the central data source object used throughout Bokeh. Although Bokeh often creates them for us transparently, there are times when it is useful to create them explicitly.

In later sections we will see features like hover tooltips, computed transforms, and CustomJS interactions that make use of the ColumnDataSource, so let's take a quick look now.

## Creating with Python Dicts¶

The ColumnDataSource can be imported from bokeh.models:

In [4]:
from bokeh.models import ColumnDataSource


The ColumnDataSource is a mapping of column names (strings) to sequences of values. Here is a simple example. The mapping is provided by passing a Python dict with string keys and simple Python lists as values. The values could also be NumPy arrays, or Pandas sequences.

NOTE: ALL the columns in a ColumnDataSource must always be the SAME length.

In [5]:
source = ColumnDataSource(data={
'x' : [1, 2, 3, 4, 5],
'y' : [3, 7, 8, 5, 1],
})


Up until now we have called functions like p.circle by passing in literal lists or arrays of data directly, when we do this, Bokeh creates a ColumnDataSource for us, automatically. But it is possible to specify a ColumnDataSource explicitly by passing it as the source argument to a glyph method. Whenever we do this, if we want a property (like "x" or "y" or "fill_color") to have a sequence of values, we pass the name of the column that we would like to use for a property:

In [6]:
p = figure(plot_width=400, plot_height=400)
p.circle('x', 'y', size=20, source=source)
show(p)

In [7]:
# Exercise: create a column data source with NumPy arrays as column values and plot it

import numpy as np


## Creating with Pandas DataFrames¶

It's also simple to create ColumnDataSource objects directly from Pandas data frames. To do this, just pass the data frame to ColumnDataSource when you create it:

In [8]:
from bokeh.sampledata.iris import flowers as df

source = ColumnDataSource(df)


Now we can use it as we did above by passing the column names to glyph methods:

In [9]:
p = figure(plot_width=400, plot_height=400)
p.circle('petal_length', 'petal_width', source=source)
show(p)

In [10]:
# Exercise: create a column data source with the autompg sample data frame and plot it

from bokeh.sampledata.autompg import autompg_clean as df


## Automatic Conversion¶

If you do not need to share data sources, it may be convenient to pass dicts, Pandas DataFrame or GroupBy objects directly to glhyph methods, without explicitly creating a ColumnDataSource. In this case, a ColumnDataSource will be created automatically.

In [11]:
from bokeh.sampledata.iris import flowers as df

p = figure(plot_width=400, plot_height=400)
p.circle('petal_length', 'petal_width', source=df)
show(p)


## Transformations¶

In addition to being configured with names of columns from data sources, glyph properties may also be configured with transform objects that represent transformations of columns. These live in the bokeh.transform module. It is important to note that when doing using these objects, the tranformations occur in the browser, not in Python.

The first transform we look at is the cumsum transform, which can generate a new sequence of values from a data source column by cumulatively summing the values in the column. This can be usefull for pie or donut type charts as seen below.

In [12]:
from math import pi
import pandas as pd
from bokeh.palettes import Category20c
from bokeh.transform import cumsum

x = { 'United States': 157, 'United Kingdom': 93, 'Japan': 89, 'China': 63,
'Germany': 44, 'India': 42, 'Italy': 40, 'Australia': 35, 'Brazil': 32,
'France': 31, 'Taiwan': 31, 'Spain': 29 }

data = pd.Series(x).reset_index(name='value').rename(columns={'index':'country'})
data['color'] = Category20c[len(x)]

# represent each value as an angle = value / total * 2pi
data['angle'] = data['value']/data['value'].sum() * 2*pi

p = figure(plot_height=350, title="Pie Chart", toolbar_location=None,
tools="hover", tooltips="@country: @value")

# use cumsum to cumulatively sum the values for start and end angles
start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'),
line_color="white", fill_color='color', legend_field='country', source=data)

p.axis.axis_label=None
p.axis.visible=False
p.grid.grid_line_color = None

show(p)


The next transform we look at is the linear_cmap transform, which can generate a new sequence of colors by applying a linear colormapping to a data source column.

In [13]:
from bokeh.transform import linear_cmap

N = 4000
data = dict(x=np.random.random(size=N) * 100,
y=np.random.random(size=N) * 100,
r=np.random.random(size=N) * 1.5)

p = figure()

# color map based on the x-coordinate
color=linear_cmap('x', 'Viridis256', 0, 100))

show(p)


Change the code above to use log_cmap and observe the results. Try changing low and high and specificying low_color and high_color.

In [14]:
# Exercise: use the corresponding factor_cmap to color map a scatter plot of the iris data set

from bokeh.sampledata.iris import flowers


# Next Section¶

Click on this link to go to the next notebook: 04 - Adding Annotations.