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)
In [3]:
medals
Out[3]:
In [4]:
bronze
Out[4]:
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]:
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)