In [1]:
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
In [2]:
import pandas as pd
In [3]:
output_notebook()
Loading BokehJS ...
In [4]:
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()
Out[4]:
Year Month Rating
0 2002 Jan 0.000000
1 2003 Jan 0.794586
2 2004 Jan 0.810332
3 2005 Jan 0.822619
4 2006 Jan 0.818533
In [5]:
acdf = pd.pivot_table(cdf, index=['Year'], columns=['Month'])
acdf.columns = acdf.columns.droplevel(0)
acdf = acdf[cdf['Month'].unique().tolist()]
acdf.head()
Out[5]:
Month Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
Year
2002 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.829429 0.772742 0.859048 0.826250 0.780417 0.766739
2003 0.794586 0.788333 0.812756 0.758246 0.798598 0.800460 0.840994 0.810782 0.839610 0.821250 0.827163 0.799866
2004 0.810332 0.825000 0.812195 0.821558 0.815828 0.803860 0.817981 0.791423 0.812903 0.808825 0.813662 0.808625
2005 0.822619 0.801692 0.826941 0.810052 0.829125 0.817249 0.817197 0.829545 0.806301 0.811946 0.796417 0.831208
2006 0.818533 0.813261 0.769806 0.814383 0.819020 0.809942 0.819776 0.802465 0.801928 0.817741 0.817387 0.817363
In [6]:
months = list(acdf.columns)
years = list(acdf.index)
In [7]:
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)
In [ ]: