from bokeh.models import ColumnDataSource, LinearColorMapper, ColorBar, BasicTicker, NumeralTickFormatter
from bokeh.transform import transform
from bokeh.plotting import figure
from bokeh.palettes import Inferno256
from bokeh.io import output_notebook, show
import pandas as pd
output_notebook()
df = pd.read_csv('acdf.csv', sep='\t')
df = pd.pivot_table(df, index=['Year'])[df.columns[1:]].reset_index()
cdf = pd.melt(df, id_vars=['Year'], value_vars=df.columns[1:], var_name='Month', value_name='Rating')
cdf['Year'] = cdf['Year'].astype(str)
cdf.head()
acdf = pd.pivot_table(cdf, index=['Year'], columns=['Month'])
acdf.columns = acdf.columns.droplevel(0)
acdf = acdf[cdf['Month'].unique().tolist()]
acdf.head()
months = list(acdf.columns)
years = list(acdf.index)
source = ColumnDataSource(cdf)
mapper = LinearColorMapper(palette=Inferno256, low=0.0, high=acdf.max().max())
color_bar = ColorBar(color_mapper=mapper, location=(0, 0),
ticker=BasicTicker(desired_num_ticks=256//12),
formatter=NumeralTickFormatter(format="%f"))
p = figure(plot_width=800, plot_height=400, toolbar_location=None,
x_range=(years), y_range=list(reversed(months)),
tools="", x_axis_location="above")
p.rect(x="Year", y="Month", width=1, height=1, source=source,
line_color=None, fill_color=transform('Rating', mapper))
p.add_layout(color_bar, 'right')
p.axis.axis_line_color = None
p.axis.major_tick_line_color = None
p.axis.major_label_standoff = 0
p.xaxis.major_label_orientation = 1.0
show(p)