from bokeh.charts import Donut, show, output_notebook, vplot
from bokeh.charts.utils import df_from_json
from bokeh.sampledata.olympics2014 import data
from bokeh.sampledata.autompg import autompg
output_notebook()
import pandas as pd
d = Donut([2, 4, 5, 2, 8])
show(d)
d = Donut(pd.Series([2, 4, 5, 2, 8], index=['a', 'b', 'c', 'd', 'e']))
show(d)
autompg.head()
d = Donut(autompg.cyl.astype(str))
show(d)
d = Donut(autompg, label='cyl', agg='count')
show(d)
d = Donut(autompg.groupby('cyl').displ.mean())
show(d)
d = Donut(autompg, label='cyl',
values='displ', agg='mean')
show(d)
Since the aggregation type isn't specified, we must provide it to the chart for use in the tooltip, otherwise it will just say "value".
d = Donut(autompg.groupby(['cyl', 'origin']).displ.mean(), hover_text='mean')
show(d)
In previous series input example we do not have the original values so we cannot size the wedges based on the mean of displacement for Cyl, then size the wedges proportionally inside of the Cyl wedge. This column labeled example can perform the right sizing, so would be preferred for any aggregated values.
d = Donut(autompg, label=['cyl', 'origin'],
values='displ', agg='mean')
show(d)
By default, this is applied to only the levels other than the first.
d = Donut(autompg, label=['cyl', 'origin'],
values='displ', agg='mean', level_spacing=0.15)
show(d)
This is applied to each level individually, including the first.
d = Donut(autompg, label=['cyl', 'origin'],
values='displ', agg='mean', level_spacing=[0.8, 0.3])
show(d)
print(data.keys())
data['data'][0]
# utilize utility to make it easy to get json/dict data converted to a dataframe
df = df_from_json(data)
df.head()
This data is in a "pivoted" format, and since the charts interface is built around referencing columns, it is more convenient to de-pivot the data.
# filter by countries with at least one medal and sort by total medals
df = df[df['total'] > 8]
df = df.sort("total", ascending=False)
olympics = pd.melt(df, id_vars=['abbr'],
value_vars=['bronze', 'silver', 'gold'],
value_name='medal_count', var_name='medal')
olympics.head()
# original example
d0 = Donut(olympics, label=['abbr', 'medal'], values='medal_count',
text_font_size='8pt', hover_text='medal_count')
show(d0)