In [1]:
from collections import OrderedDict
import numpy as np
import pandas as pd
from bokeh.charts import Bar, output_notebook, show
from bokeh.plotting import VBox
from bokeh.sampledata.olympics2014 import data as original_data

In [2]:
data = {d['abbr']: d['medals'] for d in original_data['data'] if d['medals']['total'] > 0}

countries = sorted(data.keys(), key=lambda x: data[x]['total'], reverse=True)

gold = np.array([data[abbr]['gold'] for abbr in countries], dtype=np.float)
silver = np.array([data[abbr]['silver'] for abbr in countries], dtype=np.float)
bronze = np.array([data[abbr]['bronze'] for abbr in countries], dtype=np.float)
output_notebook()
medals = OrderedDict(bronze=bronze, silver=silver, gold=gold)


BokehJS successfully loaded.

In [3]:
medals


Out[3]:
OrderedDict([('bronze', array([  7.,  10.,   8.,   7.,   4.,   4.,   1.,   5.,   2.,   1.,   4.,
         2.,   1.,   1.,   4.,   2.,   1.,   0.,   1.,   2.,   0.,   1.,
         1.,   0.,   0.,   1.])), ('silver', array([ 8.,  4.,  6.,  4.,  8.,  3.,  3.,  2.,  5.,  6.,  1.,  4.,  2.,
        0.,  2.,  3.,  1.,  0.,  2.,  1.,  3.,  0.,  0.,  0.,  1.,  0.])), ('gold', array([ 6.,  6.,  6.,  8.,  4.,  8.,  6.,  3.,  2.,  2.,  2.,  1.,  3.,
        5.,  0.,  1.,  2.,  4.,  0.,  0.,  0.,  1.,  0.,  1.,  0.,  0.]))])

In [4]:
bronze


Out[4]:
array([  7.,  10.,   8.,   7.,   4.,   4.,   1.,   5.,   2.,   1.,   4.,
         2.,   1.,   1.,   4.,   2.,   1.,   0.,   1.,   2.,   0.,   1.,
         1.,   0.,   0.,   1.])

In [5]:
bar = Bar(
    medals, countries, title="stacked, dict input", 
    xlabel="countries", ylabel="medals", legend="top_right", width=600, height=400, stacked=True)
show(bar)



In [6]:
df = pd.DataFrame(medals, index=countries)
df


Out[6]:
bronze silver gold
RUS 7 8 6
USA 10 4 6
NLD 8 6 6
NOR 7 4 8
CAN 4 8 4
DEU 4 3 8
CHE 1 3 6
FRA 5 2 3
SWE 2 5 2
AUT 1 6 2
SVN 4 1 2
JPN 2 4 1
CHN 1 2 3
BLR 1 0 5
ITA 4 2 0
CZE 2 3 1
KOR 1 1 2
POL 0 0 4
AUS 1 2 0
LVA 2 1 0
FIN 0 3 0
GBR 1 0 1
UKR 1 0 0
SVK 0 0 1
HRV 0 1 0
KAZ 1 0 0

In [7]:
bar1 = Bar(
    df, title="stacked, pandas input", xlabel="countries", ylabel="medals", 
    legend="top_right", width=600, height=400, stacked=True)

bar2 = bar = Bar(
    df, title="grouped, pandas input", xlabel="countries", ylabel="medals", 
    legend="top_right", width=600, height=400, stacked=False)

show(bar1)



In [8]:
show(bar2)