In [3]:
"""
Example from the main website gallery
http://bokeh.pydata.org/en/latest/docs/gallery/dot_chart.html
"""
#######
from bokeh.plotting import hplot, vplot, gridplot
#######
from collections import OrderedDict
from bokeh._legacy_charts import Dot, show, output_file
# create some example data
xyvalues = OrderedDict(
python=[2, 3, 7, 5, 26],
pypy=[12, 33, 47, 15, 126],
jython=[22, 43, 10, 25, 26],
)
# any of the following commented are also valid Dot inputs
#xyvalues = pd.DataFrame(xyvalues)
#xyvalues = list(xyvalues.values())
#xyvalues = np.array(list(xyvalues.values()))
output_file("all_combined_output.html")
TOOLS="resize,crosshair,pan,wheel_zoom,box_zoom,reset,previewsave"
dots = Dot(
xyvalues, cat=['lists','loops','dicts', 'gen exp', 'exceptions'],
title="Dots Example", ylabel='Performance', legend=True
)
show(dots)
In [5]:
"""
Example from the main website gallery
http://bokeh.pydata.org/en/latest/docs/gallery/cat_heatmap_chart.html
"""
from bokeh._legacy_charts import HeatMap, output_file, show
from bokeh.palettes import YlOrRd9 as palette
from bokeh.sampledata.unemployment1948 import data
# pandas magic
df = data[data.columns[:-1]]
df2 = df.set_index(df[df.columns[0]].astype(str))
df2.drop(df.columns[0], axis=1, inplace=True)
df3 = df2.transpose()
#output_file("cat_heatmap.html")
palette = palette[::-1] # Reverse the color order so dark red is highest unemployment
hm = HeatMap(df3, title="categorical heatmap", width=800, palette=palette)
#show(hm)
show(hplot(hm, dots))
In [12]:
"""
Example from the main website gallery
http://bokeh.pydata.org/en/latest/docs/gallery/iris_scatter_chart.html
"""
from collections import OrderedDict
from bokeh._legacy_charts import Scatter, output_file, show
from bokeh.sampledata.iris import flowers
# fill a data frame with the data of interest and create a groupby object
df = flowers[["petal_length", "petal_width", "species"]]
xyvalues = g = df.groupby("species")
# drop that groupby object into a dict
pdict = OrderedDict()
for i in g.groups.keys():
labels = g.get_group(i).columns
xname = labels[0]
yname = labels[1]
x = getattr(g.get_group(i), xname)
y = getattr(g.get_group(i), yname)
pdict[i] = zip(x, y)
# any of the following commented are also valid Scatter inputs
#xyvalues = pdict
#xyvalues = pd.DataFrame(xyvalues)
#xyvalues = xyvalues.values()
#xyvalues = np.array(xyvalues.values())
#output_file("iris_scatter.html")
scatter = Scatter(xyvalues, ylabel='petal_width')
show(scatter)
In [2]:
from collections import OrderedDict
import pandas as pd
from bokeh.charts import Donut, show, output_file
from bokeh.sampledata.olympics2014 import data
# throw the data into a pandas data frame
df = pd.io.json.json_normalize(data['data'])
# filter by countries with at least one medal and sort
df = df[df['medals.total'] > 8]
df = df.sort("medals.total", ascending=False)
# get the countries and we group the data by medal type
countries = df.abbr.values.tolist()
gold = df['medals.gold'].astype(float).values
silver = df['medals.silver'].astype(float).values
bronze = df['medals.bronze'].astype(float).values
# build a dict containing the grouped data
medals = OrderedDict()
medals['bronze'] = bronze
medals['silver'] = silver
medals['gold'] = gold
# any of the following commented are also valid Donut inputs
#medals = list(medals.values())
#medals = np.array(list(medals.values()))
#medals = pd.DataFrame(medals)
output_file("donut.html")
donut = Donut(medals, countries)
show(donut)
In [13]:
from collections import OrderedDict
import pandas as pd
from bokeh.charts import Bar, output_file, show
from bokeh.sampledata.olympics2014 import data
df = pd.io.json.json_normalize(data['data'])
# filter by countries with at least one medal and sort
df = df[df['medals.total'] > 0]
df = df.sort("medals.total", ascending=False)
# get the countries and we group the data by medal type
countries = df.abbr.values.tolist()
gold = df['medals.gold'].astype(float).values
silver = df['medals.silver'].astype(float).values
bronze = df['medals.bronze'].astype(float).values
# build a dict containing the grouped data
medals = OrderedDict(bronze=bronze, silver=silver, gold=gold)
# any of the following commented are also alid Bar inputs
#medals = pd.DataFrame(medals)
#medals = list(medals.values())
output_file("stacked_bar.html")
bar = Bar(medals, countries, title="Stacked bars", stacked=True)
show(bar)
In [ ]: