In [ ]:
from bokeh.charts import HeatMap, bins, output_notebook, show, vplot
from bokeh.sampledata.autompg import autompg as df
from bokeh.sampledata.unemployment1948 import data

from bokeh.palettes import RdYlGn6, RdYlGn9

import pandas as pd

In [ ]:
output_notebook()

Auto MPG Data


In [ ]:
df.head()

2D Binning

Default behavior for 2d binning is to bin the dimensions provided, then count the rows that fall into each bin. This is visualizing how the source data represents all possible combinations of mpg and displacement.


In [ ]:
hm = HeatMap(df, x=bins('mpg'), y=bins('displ'))
show(hm)

Binning and Aggregating Values

For each x and y bin, the stat is used on values. After the binning operation, the aggregated values are then binned a second time so that a discrete color can be mapped to the aggregated value.


In [ ]:
hm = HeatMap(df, x=bins('mpg'), y=bins('displ'), values='cyl', stat='mean')
show(hm)

Specifying the Number of Bins


In [ ]:
hm = HeatMap(df, x=bins('mpg'), y=bins('displ', bins=15), 
             values='cyl', stat='mean')
show(hm)

Mixing binning and non-binned data


In [ ]:
hm = HeatMap(df, x=bins('mpg'), y='cyl', values='displ', stat='mean')
show(hm)

The Size of the Glyph Can be Altered


In [ ]:
hm = HeatMap(df, y=bins('displ'), x=bins('mpg'), values='cyl', stat='mean',
             spacing_ratio=0.9)

show(hm)

Applying a Custom Palette

The number of discrete colors in the palette determines the number of bins used for applying colors.

Example with 6 Colors


In [ ]:
hm = HeatMap(df, x=bins('mpg'), y=bins('displ'), stat='mean', values='cyl',
             palette=RdYlGn6)
show(hm)

Example with 9 Colors


In [ ]:
hm = HeatMap(df, x=bins('mpg'), y=bins('displ'), stat='mean', values='cyl',
             palette=RdYlGn9)
show(hm)

Viewing Color Bins in Legend


In [ ]:
hm = HeatMap(df, x=bins('mpg'), y=bins('displ'), values='cyl',
             stat='mean', legend='top_right')
show(hm)

Build Fruit Data


In [ ]:
fruits = {'fruit': ['apples', 'apples', 'apples', 'apples', 'apples',
                    'pears', 'pears', 'pears', 'pears', 'pears',
                    'bananas', 'bananas', 'bananas', 'bananas', 'bananas'],
          'fruit_count': [4, 5, 8, 12, 4, 6, 5, 4, 8, 7, 1, 2, 4, 8, 12],
          'year': [2009, 2010, 2011, 2012, 2013, 2009, 2010, 2011, 2012, 2013, 2009, 2010,
                   2011, 2012, 2013]}
fruits['year'] = [str(yr) for yr in fruits['year']]
fruits_df = pd.DataFrame(fruits)

fruits_df.head()

Without Dimension Binning

Each x and y value are treated as the coordinates of a bin. The values column is then binned and assigned to the color attribute, so discrete colors can be assigned.


In [ ]:
hm = HeatMap(fruits, y='year', x='fruit', values='fruit_count', stat=None)
show(hm)

Unemployment Data

Prepare the Data

Current data is in a pivoted form, which is difficult to use when defining plots by specifying columns. We will use the pandas melt function to de-pivot the columns with numerical data we want to use, while keeping the categorical data.


In [ ]:
unempl_data = data.copy()
unempl_data.head()

In [ ]:
# Remove the annual column if we don't want to show the total
del unempl_data['Annual']

# Convert numerical year to strings
unempl_data['Year'] = unempl_data['Year'].astype(str)

# de-pivot all columns, except for Year, into two columns.
# One column will have the values and the second will have the labels
unempl = pd.melt(unempl_data, var_name='Month', value_name='Unemployment', id_vars=['Year'])

De-pivoted Data

We now have the 3 dimensions that we want to map to attributes of the plot.


In [ ]:
unempl.head()

In [ ]:
hm = HeatMap(unempl, x='Year', y='Month', values='Unemployment', stat=None,
             sort_dim={'x': False}, height=200, responsive=True)
show(hm)

In [ ]: