Bokeh Tutorial

05. Data Transformations

Data transformations allow you to specify a way to transform data instead of doing the transform yourself. This means that you can send your raw data to the client and the transformation will happen client side. This can be efficient becuase:

  • your numerical data might be smaller than it's color representation
  • you maybe using multiple transforms on the same data
  • you have to write less code to get the plot you want

In bokeh there are Transforms and ColorMappers that fall into this category:

In [2]:
from bokeh.sampledata.autompg import autompg
autompg.head()
Out[2]:
mpg cyl displ hp weight accel yr origin name
0 18.0 8 307.0 130 3504 12.0 70 1 chevrolet chevelle malibu
1 15.0 8 350.0 165 3693 11.5 70 1 buick skylark 320
2 18.0 8 318.0 150 3436 11.0 70 1 plymouth satellite
3 16.0 8 304.0 150 3433 12.0 70 1 amc rebel sst
4 17.0 8 302.0 140 3449 10.5 70 1 ford torino
In [16]:
from bokeh.io import output_notebook, show
output_notebook()
Loading BokehJS ...
In [12]:
from bokeh.models import ColumnDataSource
source = ColumnDataSource(autompg)
source.column_names
Out[12]:
['hp',
 'index',
 'origin',
 'yr',
 'name',
 'displ',
 'accel',
 'mpg',
 'weight',
 'cyl']
In [4]:
from bokeh.plotting import figure
In [13]:
p = figure(height=400, x_axis_label='year', y_axis_label='mpg')
p.circle(x='yr', y='mpg', alpha=0.6, size=15, source=source)
show(p)

Use Jitter to see distribution a little better

We use the explicit field specification to spell the data transform

In [19]:
from bokeh.models import Jitter
p = figure(height=400, width=800, x_axis_label='year', y_axis_label='mpg')
p.circle(x={'field': 'yr', 'transform': Jitter(width=0.2)}, y=autompg.mpg, alpha=0.6, size=15, source=source)
show(p)

Use a Linear Interpolator to size by horsepower

In [22]:
from bokeh.models import LinearInterpolator
size_mapper = LinearInterpolator(
    x=[autompg.hp.min(), autompg.hp.max()],
    y=[3, 30]
)
p = figure(height=400, width=800, x_axis_label='year', y_axis_label='mpg')
p.circle(x='yr', y=autompg.mpg, alpha=0.6, size={'field': 'hp', 'transform': size_mapper}, source=source)
show(p)

Exercise: Use a Color Mapper to color by weight

In [ ]:
from bokeh.models import LinearColorMapper
from bokeh.palettes import Viridis256
color_mapper = LinearColorMapper