{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "parameters" ] }, "outputs": [], "source": [ "flex_title = \"Bokeh plots\"\n", "flex_source_code = \"https://github.com/danielfrg/jupyter-flex/blob/master/examples/plots/bokeh.ipynb\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "meta" ] }, "outputs": [], "source": [ "import datetime\n", "\n", "import numpy as np\n", "import pandas as pd\n", "\n", "from bokeh.sampledata.iris import flowers as df\n", "from bokeh.plotting import figure, show, output_notebook\n", "output_notebook()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from bokeh.models import ColumnDataSource\n", "from bokeh.palettes import Category10\n", "from bokeh.transform import factor_cmap" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "size=600" ] }, "source": [ "## Column" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Species" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "chart" ] }, "outputs": [], "source": [ "source = ColumnDataSource(df)\n", "color_map = factor_cmap('species', palette=Category10[3], factors=sorted(df.species.unique()))\n", "\n", "tooltips = [\n", " (\"Sepal Width\", \"@sepal_width\"),\n", " (\"Sepal Length\", \"@sepal_length\"),\n", " (\"Species\", \"@species\"),\n", "]\n", "\n", "p = figure(tooltips=tooltips, sizing_mode=\"stretch_both\")\n", "# p = figure(tooltips=tooltips)\n", "\n", "p.circle(source=source, x='sepal_length', y='sepal_width', color=color_map, legend_field=\"species\", alpha=0.5, size=10)\n", "\n", "p.legend.title = 'Species'\n", "p.xaxis.axis_label = 'Sepal Length'\n", "p.yaxis.axis_label = 'Sepal Width'\n", "\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "size=400" ] }, "source": [ "## Column 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Species (Quantile)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "groups = df.groupby(\"species\").sepal_length.quantile(np.arange(0, 1, 0.02))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "groups = groups.reset_index().rename(columns={\"level_1\": \"quantile\"})" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "chart" ] }, "outputs": [], "source": [ "source = ColumnDataSource(groups)\n", "color_map = factor_cmap('species', palette=Category10[3], factors=sorted(df.species.unique()))\n", "\n", "tooltips = [\n", " (\"Sepal Length\", \"@sepal_length\"),\n", " (\"Quantile\", \"@quantile\"),\n", " (\"Species\", \"@species\"),\n", "]\n", "\n", "p = figure(tooltips=tooltips, sizing_mode=\"stretch_both\")\n", "# p = figure(tooltips=tooltips)\n", "\n", "p.circle(source=source, x='quantile', y='sepal_length', color=color_map, legend_field=\"species\", alpha=0.5, size=10)\n", "\n", "p.legend.title = 'Species'\n", "p.xaxis.axis_label = 'Quantile'\n", "p.yaxis.axis_label = 'Sepal Length'\n", "p.legend.location = \"top_left\"\n", "\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Petal Width" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from bokeh.palettes import Greens" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cuts = pd.cut(df.petal_width, 5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df[\"cuts_str\"] = cuts.astype(str)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df[\"cuts_mid\"] = cuts.apply(lambda x: x.mid).astype(str)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "source = ColumnDataSource(df)\n", "color_map = factor_cmap('cuts_mid', palette=Greens[7][:-2], factors=sorted(df.cuts_mid.unique(), reverse=True))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "chart" ] }, "outputs": [], "source": [ "tooltips = [\n", " (\"Sepal Width\", \"@sepal_width\"),\n", " (\"Sepal Length\", \"@sepal_length\"),\n", " (\"Petal Width interval\", \"@cuts_str\"),\n", " (\"Species\", \"@species\"),\n", "]\n", "\n", "p = figure(tooltips=tooltips, sizing_mode=\"stretch_both\")\n", "# p = figure(tooltips=tooltips)\n", "\n", "p.circle(source=source, x='sepal_length', y='sepal_width', color=color_map, legend_field=\"cuts_str\", alpha=0.8, size=10)\n", "\n", "p.legend.title = 'Petal Width'\n", "p.xaxis.axis_label = 'Sepal Length'\n", "p.yaxis.axis_label = 'Sepal Width'\n", "\n", "show(p)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "celltoolbar": "Tags", "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }