{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "*Note: All output_file() calls have been replaced with output_notebook() so that plots will display inline.*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# The Basics of Bokeh" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Your First Plot" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from bokeh.plotting import figure, output_notebook, show\n", "from bokeh.models.tools import LassoSelectTool, CrosshairTool, HoverTool\n", "output_notebook()\n", "\n", "x = [1, 3, 5, 7]\n", "y = [2, 4, 6, 8]\n", "\n", "p = figure()\n", "\n", "p.circle(x, y, size=10, color='red', legend='circle')\n", "p.line(x, y, color='blue', legend='line')\n", "p.triangle(y, x, color='yellow', size=10, legend='triangle')\n", "\n", "p.legend.click_policy='hide'\n", "\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Bokeh and Pandas: Exploring the WWII THOR Dataset" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Loading Data in Pandas" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "df = pd.read_csv('thor_wwii.csv')\n", "print(df)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.columns.tolist()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The Bokeh ColumnDataSource" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "import pandas as pd\n", "from bokeh.plotting import figure, output_notebook, show\n", "from bokeh.models import ColumnDataSource\n", "from bokeh.models.tools import HoverTool\n", "output_notebook()\n", "\n", "df = pd.read_csv('thor_wwii.csv')\n", "\n", "sample = df.sample(50)\n", "source = ColumnDataSource(sample)\n", "\n", "p = figure()\n", "p.circle(x='TOTAL_TONS', y='AC_ATTACKING', \n", " source=source, \n", " size=10, color='green')\n", "\n", "p.title.text = 'Attacking Aircraft and Munitions Dropped'\n", "p.xaxis.axis_label = 'Tons of Munitions Dropped'\n", "p.yaxis.axis_label = 'Number of Attacking Aircraft'\n", "\n", "hover = HoverTool()\n", "hover.tooltips=[\n", " ('Attack Date', '@MSNDATE'),\n", " ('Attacking Aircraft', '@AC_ATTACKING'),\n", " ('Tons of Munitions', '@TOTAL_TONS'),\n", " ('Type of Aircraft', '@AIRCRAFT_NAME')\n", "]\n", "\n", "p.add_tools(hover)\n", "\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Categorical Data and Bar Charts: Munitions Dropped by Country" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from bokeh.plotting import figure, output_notebook, show\n", "from bokeh.models import ColumnDataSource\n", "from bokeh.models.tools import HoverTool\n", "\n", "from bokeh.palettes import Spectral5\n", "from bokeh.transform import factor_cmap\n", "output_notebook()\n", "\n", "df = pd.read_csv('thor_wwii.csv')\n", "\n", "grouped = df.groupby('COUNTRY_FLYING_MISSION')['TOTAL_TONS', 'TONS_HE', 'TONS_IC', 'TONS_FRAG'].sum()\n", "grouped = grouped / 1000\n", "\n", "source = ColumnDataSource(grouped)\n", "countries = source.data['COUNTRY_FLYING_MISSION'].tolist()\n", "p = figure(x_range=countries)\n", "\n", "color_map = factor_cmap(field_name='COUNTRY_FLYING_MISSION', \n", " palette=Spectral5, factors=countries)\n", "\n", "p.vbar(x='COUNTRY_FLYING_MISSION', top='TOTAL_TONS', source=source, width=0.70, color=color_map)\n", "\n", "p.title.text ='Munitions Dropped by Allied Country'\n", "p.xaxis.axis_label = 'Country'\n", "p.yaxis.axis_label = 'Kilotons of Munitions'\n", "\n", "hover = HoverTool()\n", "hover.tooltips = [\n", " (\"Totals\", \"@TONS_HE High Explosive / @TONS_IC Incendiary / @TONS_FRAG \tFragmentation\")]\n", "\n", "hover.mode = 'vline'\n", "\n", "p.add_tools(hover)\n", "\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Stacked Bar Charts and Sub-sampling Data: Types of Munitions Dropped by Country" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from bokeh.plotting import figure, output_notebook, show\n", "from bokeh.models import ColumnDataSource\n", "from bokeh.palettes import Spectral3\n", "output_notebook()\n", "\n", "df = pd.read_csv('thor_wwii.csv')\n", "\n", "filter = df['COUNTRY_FLYING_MISSION'].isin(('USA','GREAT BRITAIN'))\n", "df = df[filter]\n", "\n", "grouped = df.groupby('COUNTRY_FLYING_MISSION')['TONS_IC', 'TONS_FRAG', 'TONS_HE'].sum()\n", "grouped = grouped / 1000\n", "\n", "source = ColumnDataSource(grouped)\n", "countries = source.data['COUNTRY_FLYING_MISSION'].tolist()\n", "p = figure(x_range=countries)\n", "\n", "p.vbar_stack(stackers=['TONS_HE', 'TONS_FRAG', 'TONS_IC'], \n", " x='COUNTRY_FLYING_MISSION', source=source, \n", " legend = ['High Explosive', 'Fragmentation', 'Incendiary'],\n", " width=0.5, color=Spectral3)\n", "\n", "p.title.text ='Types of Munitions Dropped by Allied Country'\n", "p.legend.location = 'top_left'\n", "\n", "p.xaxis.axis_label = 'Country'\n", "p.xgrid.grid_line_color = None\t#remove the x grid lines\n", "\n", "p.yaxis.axis_label = 'Kilotons of Munitions'\n", "\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Time-Series, Annotations, and Multiple Plots: Bombing Operations over Time" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from bokeh.plotting import figure, output_notebook, show\n", "from bokeh.models import ColumnDataSource\n", "from bokeh.palettes import Spectral3\n", "output_notebook()\n", "\n", "df = pd.read_csv('thor_wwii.csv')\n", "\n", "#make sure MSNDATE is a datetime format\n", "df['MSNDATE'] = pd.to_datetime(df['MSNDATE'], format='%m/%d/%Y')\n", "\n", "grouped = df.groupby('MSNDATE')['TOTAL_TONS', 'TONS_IC', 'TONS_FRAG'].sum()\n", "grouped = grouped/1000\n", "\n", "source = ColumnDataSource(grouped)\n", "\n", "p = figure(x_axis_type='datetime')\n", "\n", "p.line(x='MSNDATE', y='TOTAL_TONS', line_width=2, source=source, legend='All Munitions')\n", "p.line(x='MSNDATE', y='TONS_FRAG', line_width=2, source=source, color=Spectral3[1], legend='Fragmentation')\n", "p.line(x='MSNDATE', y='TONS_IC', line_width=2, source=source, color=Spectral3[2], legend='Incendiary')\n", "\n", "p.yaxis.axis_label = 'Kilotons of Munitions Dropped'\n", "\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Resampling Time-Series Data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from bokeh.plotting import figure, output_notebook, show\n", "from bokeh.models import ColumnDataSource\n", "from bokeh.palettes import Spectral3\n", "output_notebook()\n", "\n", "df = pd.read_csv('thor_wwii.csv')\n", "\n", "#make sure MSNDATE is a datetime format\n", "df['MSNDATE'] = pd.to_datetime(df['MSNDATE'], format='%m/%d/%Y')\n", "\n", "grouped = df.groupby(pd.Grouper(key='MSNDATE', freq='M'))['TOTAL_TONS', 'TONS_IC', 'TONS_FRAG'].sum()\n", "grouped = grouped/1000\n", "\n", "source = ColumnDataSource(grouped)\n", "\n", "p = figure(x_axis_type='datetime')\n", "\n", "p.line(x='MSNDATE', y='TOTAL_TONS', line_width=2, source=source, legend='All Munitions')\n", "p.line(x='MSNDATE', y='TONS_FRAG', line_width=2, source=source, color=Spectral3[1], legend='Fragmentation')\n", "p.line(x='MSNDATE', y='TONS_IC', line_width=2, source=source, color=Spectral3[2], legend='Incendiary')\n", "\n", "p.yaxis.axis_label = 'Kilotons of Munitions Dropped'\n", "\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Annotating Trends in Plots" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from bokeh.plotting import figure, output_file, show\n", "from bokeh.models import ColumnDataSource\n", "from datetime import datetime\n", "from bokeh.palettes import Spectral3\n", "output_file('eto_operations.html')\n", "\n", "df = pd.read_csv('thor_wwii.csv')\n", "\n", "#filter for the European Theater of Operations\n", "filter = df['THEATER']=='ETO'\n", "df = df[filter]\n", "\n", "df['MSNDATE'] = pd.to_datetime(df['MSNDATE'], format='%m/%d/%Y')\n", "group = df.groupby(pd.Grouper(key='MSNDATE', freq='M'))['TOTAL_TONS', 'TONS_IC', 'TONS_FRAG'].sum()\n", "group = group / 1000\n", "\n", "source = ColumnDataSource(group)\n", "\n", "p = figure(x_axis_type=\"datetime\")\n", "\n", "p.line(x='MSNDATE', y='TOTAL_TONS', line_width=2, source=source, legend='All Munitions')\n", "p.line(x='MSNDATE', y='TONS_FRAG', line_width=2, source=source, color=Spectral3[1], legend='Fragmentation')\n", "p.line(x='MSNDATE', y='TONS_IC', line_width=2, source=source, color=Spectral3[2], legend='Incendiary')\n", "\n", "p.title.text = 'European Theater of Operations'\n", "\n", "p.yaxis.axis_label = 'Kilotons of Munitions Dropped'\n", "\n", "show(p)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from bokeh.plotting import figure, output_notebook, show\n", "from bokeh.models import ColumnDataSource\n", "from bokeh.models import BoxAnnotation, Label\n", "from datetime import datetime\n", "from bokeh.palettes import Spectral3\n", "output_notebook()\n", "\n", "df = pd.read_csv('thor_wwii.csv')\n", "\n", "#filter for the European Theater of Operations\n", "filter = df['THEATER']=='ETO'\n", "df = df[filter]\n", "\n", "df['MSNDATE'] = pd.to_datetime(df['MSNDATE'], format='%m/%d/%Y')\n", "group = df.groupby(pd.Grouper(key='MSNDATE', freq='M'))['TOTAL_TONS', 'TONS_IC', 'TONS_FRAG'].sum()\n", "group = group / 1000\n", "\n", "source = ColumnDataSource(group)\n", "\n", "p = figure(x_axis_type=\"datetime\")\n", "\n", "p.line(x='MSNDATE', y='TOTAL_TONS', line_width=2, source=source, legend='All Munitions')\n", "p.line(x='MSNDATE', y='TONS_FRAG', line_width=2, source=source, color=Spectral3[1], legend='Fragmentation')\n", "p.line(x='MSNDATE', y='TONS_IC', line_width=2, source=source, color=Spectral3[2], legend='Incendiary')\n", "\n", "p.title.text = 'European Theater of Operations'\n", "\n", "p.yaxis.axis_label = 'Kilotons of Munitions Dropped'\n", "\n", "box_left = pd.to_datetime('6-6-1944')\n", "box_right = pd.to_datetime('16-12-1944')\n", "\n", "box = BoxAnnotation(left=box_left, right=box_right,\n", " line_width=1, line_color='black', line_dash='dashed',\n", " fill_alpha=0.2, fill_color='orange')\n", "\n", "p.add_layout(box)\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Spatial Data: Mapping Target Locations" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from bokeh.plotting import figure, output_notebook, show\n", "from bokeh.models import ColumnDataSource, Range1d\n", "from bokeh.layouts import layout\n", "from bokeh.palettes import Spectral3\n", "from bokeh.tile_providers import CARTODBPOSITRON\n", "from pyproj import Proj, transform\n", "from bokeh.models.tools import HoverTool\n", "\n", "def LongLat_to_EN(long, lat):\n", " try:\n", " easting, northing = transform(\n", " Proj(init='epsg:4326'), Proj(init='epsg:3857'), long, lat)\n", " return easting, northing\n", " except:\n", " return None, None\n", "\n", "df = pd.read_csv('thor_wwii.csv')\n", "#convert all lat/long to webmercator and store in new column\n", "df['E'], df['N'] = zip(*df.apply(lambda x: LongLat_to_EN(x['TGT_LONGITUDE'], x['TGT_LATITUDE']), axis=1))\n", "\n", "grouped = df.groupby(['E', 'N'])['TONS_FRAG', 'TONS_IC'].sum().reset_index()\n", "\n", "filter = grouped['TONS_FRAG']!=0\n", "grouped = grouped[filter]\n", "\n", "source = ColumnDataSource(grouped)\n", "\n", "left = -2150000\n", "right = 18000000\n", "bottom = -5300000\n", "top = 11000000\n", "\n", "p = figure(x_range=Range1d(left, right), y_range=Range1d(bottom, top))\n", "p.add_tile(CARTODBPOSITRON)\n", "\n", "p.circle(x='E', y='N', source=source, line_color='grey', fill_color=Spectral3[1])\n", "\n", "p.axis.visible = False\n", "\n", "hover = HoverTool(tooltips=[\n", " (\"Fragmentation Bombs\", \"@TONS_FRAG tons\")\n", "])\n", "\n", "p.add_tools(hover)\n", "\n", "\n", "output_notebook()\n", "show(p)" ] } ], "metadata": { "kernelspec": { "display_name": "Python [conda env:bokeh-env]", "language": "python", "name": "conda-env-bokeh-env-py" }, "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.6.4" } }, "nbformat": 4, "nbformat_minor": 2 }