# coding: utf-8
#
#
#
#
#
#
# |
#
# 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:
# * Jitter
# * LinearInterpolator
# * StepInterpolator
# * [LinearColorMapper](http://bokeh.pydata.org/en/latest/docs/reference/models/mappers.html#bokeh.models.mappers.LinearColorMapper)
# * [LogColorMapper](http://bokeh.pydata.org/en/latest/docs/reference/models/mappers.html#bokeh.models.mappers.LogColorMapper)
# In[2]:
from bokeh.sampledata.autompg import autompg
autompg.head()
# In[16]:
from bokeh.io import output_notebook, show
output_notebook()
# In[12]:
from bokeh.models import ColumnDataSource
source = ColumnDataSource(autompg)
source.column_names
# 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