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
output_notebook()
df.head()
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.
hm = HeatMap(df, x=bins('mpg'), y=bins('displ'))
show(hm)
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.
hm = HeatMap(df, x=bins('mpg'), y=bins('displ'), values='cyl', stat='mean')
show(hm)
hm = HeatMap(df, x=bins('mpg'), y=bins('displ', bins=15),
values='cyl', stat='mean')
show(hm)
hm = HeatMap(df, x=bins('mpg'), y='cyl', values='displ', stat='mean')
show(hm)
hm = HeatMap(df, y=bins('displ'), x=bins('mpg'), values='cyl', stat='mean',
spacing_ratio=0.9)
show(hm)
hm = HeatMap(df, x=bins('mpg'), y=bins('displ'), stat='mean', values='cyl',
palette=RdYlGn6)
show(hm)
hm = HeatMap(df, x=bins('mpg'), y=bins('displ'), stat='mean', values='cyl',
palette=RdYlGn9)
show(hm)
hm = HeatMap(df, x=bins('mpg'), y=bins('displ'), values='cyl',
stat='mean', legend='top_right')
show(hm)
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()
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.
hm = HeatMap(fruits, y='year', x='fruit', values='fruit_count', stat=None)
show(hm)
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.
unempl_data = data.copy()
unempl_data.head()
# 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'])
We now have the 3 dimensions that we want to map to attributes of the plot.
unempl.head()
hm = HeatMap(unempl, x='Year', y='Month', values='Unemployment', stat=None,
sort_dim={'x': False}, height=200, responsive=True)
show(hm)