flex_title = "Bokeh plots"
flex_subtitle = "built using jupyter-flex"
flex_source_link = "https://github.com/danielfrg/jupyter-flex/blob/master/examples/plots/bokeh.ipynb"
flex_include_source = True
import datetime
import numpy as np
import pandas as pd
from bokeh.sampledata.iris import flowers as df
from bokeh.models import ColumnDataSource
from bokeh.palettes import Category10
from bokeh.transform import factor_cmap
source = ColumnDataSource(df)
color_map = factor_cmap('species', palette=Category10[3], factors=sorted(df.species.unique()))
tooltips = [
("Sepal Width", "@sepal_width"),
("Sepal Length", "@sepal_length"),
("Species", "@species"),
]
p = figure(tooltips=tooltips, sizing_mode="stretch_both")
# p = figure(tooltips=tooltips)
p.circle(source=source, x='sepal_length', y='sepal_width', color=color_map, legend_field="species", alpha=0.5, size=10)
p.legend.title = 'Species'
p.xaxis.axis_label = 'Sepal Length'
p.yaxis.axis_label = 'Sepal Width'
show(p)
groups = df.groupby("species").sepal_length.quantile(np.arange(0, 1, 0.02))
groups = groups.reset_index().rename(columns={"level_1": "quantile"})
source = ColumnDataSource(groups)
color_map = factor_cmap('species', palette=Category10[3], factors=sorted(df.species.unique()))
tooltips = [
("Sepal Length", "@sepal_length"),
("Quantile", "@quantile"),
("Species", "@species"),
]
p = figure(tooltips=tooltips, sizing_mode="stretch_both")
# p = figure(tooltips=tooltips)
p.circle(source=source, x='quantile', y='sepal_length', color=color_map, legend_field="species", alpha=0.5, size=10)
p.legend.title = 'Species'
p.xaxis.axis_label = 'Quantile'
p.yaxis.axis_label = 'Sepal Length'
p.legend.location = "top_left"
show(p)
from bokeh.palettes import Greens
cuts = pd.cut(df.petal_width, 5)
df["cuts_str"] = cuts.astype(str)
df["cuts_mid"] = cuts.apply(lambda x: x.mid).astype(str)
source = ColumnDataSource(df)
color_map = factor_cmap('cuts_mid', palette=Greens[7][:-2], factors=sorted(df.cuts_mid.unique(), reverse=True))
tooltips = [
("Sepal Width", "@sepal_width"),
("Sepal Length", "@sepal_length"),
("Petal Width interval", "@cuts_str"),
("Species", "@species"),
]
p = figure(tooltips=tooltips, sizing_mode="stretch_both")
# p = figure(tooltips=tooltips)
p.circle(source=source, x='sepal_length', y='sepal_width', color=color_map, legend_field="cuts_str", alpha=0.8, size=10)
p.legend.title = 'Petal Width'
p.xaxis.axis_label = 'Sepal Length'
p.yaxis.axis_label = 'Sepal Width'
show(p)