{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Time-Series Visualization using bokeh/plotly - Neerja Doshi\n", "For this assignment, Bokeh has been used as the package for creating plots." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/neerjadoshi/anaconda2/lib/python2.7/site-packages/bokeh/util/deprecation.py:34: BokehDeprecationWarning: \n", "The bokeh.charts API has moved to a separate 'bkcharts' package.\n", "\n", "This compatibility shim will remain until Bokeh 1.0 is released.\n", "After that, if you want to use this API you will have to install\n", "the bkcharts package explicitly.\n", "\n", " warn(message)\n" ] } ], "source": [ "# import packages\n", "import pandas as pd\n", "import numpy as np\n", "from bokeh.plotting import figure, show, output_file, output_notebook\n", "from bokeh.palettes import Spectral11, colorblind, Inferno, BuGn, brewer\n", "import datetime\n", "from bokeh.charts import HeatMap\n", "from bokeh.models import HoverTool, value, LabelSet, Legend, ColumnDataSource,LinearColorMapper,BasicTicker, PrintfTickFormatter, ColorBar" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
DateCategoryIncidntNum
02014-02-01BURGLARY506
12007-02-01VANDALISM531
22012-07-01BURGLARY522
32013-07-01LARCENY/THEFT3318
42010-08-01VANDALISM694
\n", "
" ], "text/plain": [ " Date Category IncidntNum\n", "0 2014-02-01 BURGLARY 506\n", "1 2007-02-01 VANDALISM 531\n", "2 2012-07-01 BURGLARY 522\n", "3 2013-07-01 LARCENY/THEFT 3318\n", "4 2010-08-01 VANDALISM 694" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Load the data\n", "data = pd.read_csv('Monthly_Property_Crime_2005_to_2015.csv', parse_dates=['Date'])\n", "data.head()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(Timestamp('2005-01-01 00:00:00'), Timestamp('2015-12-01 00:00:00'))" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.Date.min(), data.Date.max()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "STOLEN PROPERTY 132\n", "VEHICLE THEFT 132\n", "VANDALISM 132\n", "LARCENY/THEFT 132\n", "ARSON 132\n", "BURGLARY 132\n", "Name: Category, dtype: int64" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.Category.value_counts()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
DateCategoryIncidntNumYearMonth
02014-02-01BURGLARY50620142
12007-02-01VANDALISM53120072
22012-07-01BURGLARY52220127
32013-07-01LARCENY/THEFT331820137
42010-08-01VANDALISM69420108
\n", "
" ], "text/plain": [ " Date Category IncidntNum Year Month\n", "0 2014-02-01 BURGLARY 506 2014 2\n", "1 2007-02-01 VANDALISM 531 2007 2\n", "2 2012-07-01 BURGLARY 522 2012 7\n", "3 2013-07-01 LARCENY/THEFT 3318 2013 7\n", "4 2010-08-01 VANDALISM 694 2010 8" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data['Year'] = data.Date.apply(lambda x: x.year)\n", "data['Month'] = data.Date.apply(lambda x: x.month)\n", "data.head()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
DateCategoryIncidntNumYearMonth
1982005-01-01ARSON2120051
2152005-02-01ARSON1720052
362005-03-01ARSON1920053
3132005-04-01ARSON2420054
2612005-05-01ARSON1320055
\n", "
" ], "text/plain": [ " Date Category IncidntNum Year Month\n", "198 2005-01-01 ARSON 21 2005 1\n", "215 2005-02-01 ARSON 17 2005 2\n", "36 2005-03-01 ARSON 19 2005 3\n", "313 2005-04-01 ARSON 24 2005 4\n", "261 2005-05-01 ARSON 13 2005 5" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "burglary = data[data.Category == 'BURGLARY'].sort_values(['Date'])\n", "stolen_property = data[data.Category == 'STOLEN PROPERTY'].sort_values(['Date'])\n", "vehicle_theft = data[data.Category == 'VEHICLE THEFT'].sort_values(['Date'])\n", "vandalism = data[data.Category == 'VANDALISM'].sort_values(['Date'])\n", "larceny = data[data.Category == 'LARCENY/THEFT'].sort_values(['Date'])\n", "arson = data[data.Category == 'ARSON'].sort_values(['Date'])\n", "arson.head()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " Loading BokehJS ...\n", "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "\n", "(function(root) {\n", " function now() {\n", " return new Date();\n", " }\n", "\n", " var force = true;\n", "\n", " if (typeof (root._bokeh_onload_callbacks) === \"undefined\" || force === true) {\n", " root._bokeh_onload_callbacks = [];\n", " root._bokeh_is_loading = undefined;\n", " }\n", "\n", "\n", " \n", " if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n", " root._bokeh_timeout = Date.now() + 5000;\n", " root._bokeh_failed_load = false;\n", " }\n", "\n", " var NB_LOAD_WARNING = {'data': {'text/html':\n", " \"
\\n\"+\n", " \"

\\n\"+\n", " \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n", " \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n", " \"

\\n\"+\n", " \"\\n\"+\n", " \"\\n\"+\n", " \"from bokeh.resources import INLINE\\n\"+\n", " \"output_notebook(resources=INLINE)\\n\"+\n", " \"\\n\"+\n", " \"
\"}};\n", "\n", " function display_loaded() {\n", " if (root.Bokeh !== undefined) {\n", " var el = document.getElementById(\"2a1b894c-d877-494d-bd03-f083d6f9aea5\");\n", " if (el != null) {\n", " el.textContent = \"BokehJS \" + Bokeh.version + \" successfully loaded.\";\n", " }\n", " } else if (Date.now() < root._bokeh_timeout) {\n", " setTimeout(display_loaded, 100)\n", " }\n", " }\n", "\n", "\n", " function run_callbacks() {\n", " try {\n", " root._bokeh_onload_callbacks.forEach(function(callback) { callback() });\n", " }\n", " finally {\n", " delete root._bokeh_onload_callbacks\n", " }\n", " console.info(\"Bokeh: all callbacks have finished\");\n", " }\n", "\n", " function load_libs(js_urls, callback) {\n", " root._bokeh_onload_callbacks.push(callback);\n", " if (root._bokeh_is_loading > 0) {\n", " console.log(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n", " return null;\n", " }\n", " if (js_urls == null || js_urls.length === 0) {\n", " run_callbacks();\n", " return null;\n", " }\n", " console.log(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n", " root._bokeh_is_loading = js_urls.length;\n", " for (var i = 0; i < js_urls.length; i++) {\n", " var url = js_urls[i];\n", " var s = document.createElement('script');\n", " s.src = url;\n", " s.async = false;\n", " s.onreadystatechange = s.onload = function() {\n", " root._bokeh_is_loading--;\n", " if (root._bokeh_is_loading === 0) {\n", " console.log(\"Bokeh: all BokehJS libraries loaded\");\n", " run_callbacks()\n", " }\n", " };\n", " s.onerror = function() {\n", " console.warn(\"failed to load library \" + url);\n", " };\n", " console.log(\"Bokeh: injecting script tag for BokehJS library: \", url);\n", " document.getElementsByTagName(\"head\")[0].appendChild(s);\n", " }\n", " };var element = document.getElementById(\"2a1b894c-d877-494d-bd03-f083d6f9aea5\");\n", " if (element == null) {\n", " console.log(\"Bokeh: ERROR: autoload.js configured with elementid '2a1b894c-d877-494d-bd03-f083d6f9aea5' but no matching script tag was found. \")\n", " return false;\n", " }\n", "\n", " var js_urls = [\"https://cdn.pydata.org/bokeh/release/bokeh-0.12.7.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.7.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-tables-0.12.7.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-gl-0.12.7.min.js\"];\n", "\n", " var inline_js = [\n", " function(Bokeh) {\n", " Bokeh.set_log_level(\"info\");\n", " },\n", " \n", " function(Bokeh) {\n", " \n", " },\n", " \n", " function(Bokeh) {\n", " \n", " document.getElementById(\"2a1b894c-d877-494d-bd03-f083d6f9aea5\").textContent = \"BokehJS is loading...\";\n", " },\n", " function(Bokeh) {\n", " console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-0.12.7.min.css\");\n", " Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-0.12.7.min.css\");\n", " console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.7.min.css\");\n", " Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.7.min.css\");\n", " console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-tables-0.12.7.min.css\");\n", " Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-tables-0.12.7.min.css\");\n", " }\n", " ];\n", "\n", " function run_inline_js() {\n", " \n", " if ((root.Bokeh !== undefined) || (force === true)) {\n", " for (var i = 0; i < inline_js.length; i++) {\n", " inline_js[i].call(root, root.Bokeh);\n", " }if (force === true) {\n", " display_loaded();\n", " }} else if (Date.now() < root._bokeh_timeout) {\n", " setTimeout(run_inline_js, 100);\n", " } else if (!root._bokeh_failed_load) {\n", " console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n", " root._bokeh_failed_load = true;\n", " } else if (force !== true) {\n", " var cell = $(document.getElementById(\"2a1b894c-d877-494d-bd03-f083d6f9aea5\")).parents('.cell').data().cell;\n", " cell.output_area.append_execute_result(NB_LOAD_WARNING)\n", " }\n", "\n", " }\n", "\n", " if (root._bokeh_is_loading === 0) {\n", " console.log(\"Bokeh: BokehJS loaded, going straight to plotting\");\n", " run_inline_js();\n", " } else {\n", " load_libs(js_urls, function() {\n", " console.log(\"Bokeh: BokehJS plotting callback run at\", now());\n", " run_inline_js();\n", " });\n", " }\n", "}(window));" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "output_notebook()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Bar Chart\n", "This is used to analyse the average crimes per month. All the months have between 600-800 average crimes, with February being the least." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
MonthIncidntNumYear
01702.5606062010.0
12620.4090912010.0
23707.7121212010.0
34700.0606062010.0
45708.3787882010.0
\n", "
" ], "text/plain": [ " Month IncidntNum Year\n", "0 1 702.560606 2010.0\n", "1 2 620.409091 2010.0\n", "2 3 707.712121 2010.0\n", "3 4 700.060606 2010.0\n", "4 5 708.378788 2010.0" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "temp_df = data.groupby(['Month']).mean().reset_index()\n", "temp_df.head()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "TOOLS = \"hover,save,pan,box_zoom,reset,wheel_zoom,tap\"\n", "p = figure(plot_height=350,\n", "# plot_width=1000,\n", " title=\"Average Number of Crimes by Month\",\n", " tools=TOOLS,\n", " toolbar_location='above')\n", "\n", "p.vbar(x=temp_df.Month, top=temp_df.IncidntNum, width=0.9)\n", "\n", "p.y_range.start = 0\n", "p.x_range.range_padding = 0.1\n", "p.xgrid.grid_line_color = None\n", "p.axis.minor_tick_line_color = None\n", "p.outline_line_color = None\n", "p.xaxis.axis_label = 'Month'\n", "p.yaxis.axis_label = 'Average Crimes'\n", "p.select_one(HoverTool).tooltips = [\n", " ('month', '@x'),\n", " ('Number of crimes', '@top'),\n", "]\n", "output_file(\"barchart.html\", title=\"barchart\")\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Line Chart\n", "This plot shows the trend in number of crimes over the years. It can be seen that the crime rate decreased from 2005-2010, with 2010 having the lowest crime rate. From then on, it has kept increasing steadily with 2015 having the highest number of crimes." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
YearIncidntNumMonth
0200558368468
1200650150468
2200746023468
3200845647468
4200944633468
\n", "
" ], "text/plain": [ " Year IncidntNum Month\n", "0 2005 58368 468\n", "1 2006 50150 468\n", "2 2007 46023 468\n", "3 2008 45647 468\n", "4 2009 44633 468" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "temp_df = data.groupby(['Year']).sum().reset_index()\n", "temp_df.head()" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "TOOLS = 'save,pan,box_zoom,reset,wheel_zoom,hover'\n", "p = figure(title=\"Year-wise total number of crimes\", y_axis_type=\"linear\", plot_height = 400,\n", " tools = TOOLS, plot_width = 800)\n", "p.xaxis.axis_label = 'Year'\n", "p.yaxis.axis_label = 'Total Crimes'\n", "p.circle(2010, temp_df.IncidntNum.min(), size = 10, color = 'red')\n", "\n", "p.line(temp_df.Year, temp_df.IncidntNum,line_color=\"purple\", line_width = 3)\n", "p.select_one(HoverTool).tooltips = [\n", " ('year', '@x'),\n", " ('Number of crimes', '@y'),\n", "]\n", "\n", "output_file(\"line_chart.html\", title=\"Line Chart\")\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Stacked bar chart\n", "This chart explores the distribution of crimes among the various categories over the years. In particular, larceny/theft are the most frequently occuring crimes, while stolen property occur the least. 2005 saw a high number of vehicle thefts, which reduced quite a bit subsequently." ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CategoryYearARSONBURGLARYLARCENY/THEFTSTOLEN PROPERTYVANDALISMVEHICLE THEFT
02005231707125319540701318194
1200624070042735257576887291
2200724654542577052775666460
3200824856792580751873426053
4200922253792558566076045183
\n", "
" ], "text/plain": [ "Category Year ARSON BURGLARY LARCENY/THEFT STOLEN PROPERTY VANDALISM \\\n", "0 2005 231 7071 25319 540 7013 \n", "1 2006 240 7004 27352 575 7688 \n", "2 2007 246 5454 25770 527 7566 \n", "3 2008 248 5679 25807 518 7342 \n", "4 2009 222 5379 25585 660 7604 \n", "\n", "Category VEHICLE THEFT \n", "0 18194 \n", "1 7291 \n", "2 6460 \n", "3 6053 \n", "4 5183 " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wide = data.pivot(index='Date', columns='Category', values='IncidntNum')\n", "wide.reset_index(inplace=True)\n", "wide['Year'] = wide.Date.apply(lambda x: x.year)\n", "wide['Month'] = wide.Date.apply(lambda x: x.month)\n", "\n", "temp_df = wide.groupby(['Year']).sum().reset_index()\n", "temp_df.head()\n", "cats = ['ARSON','BURGLARY','LARCENY/THEFT','STOLEN PROPERTY','VANDALISM','VEHICLE THEFT'] \n", "temp_df.drop(['Month'], axis = 1, inplace=True)\n", "temp_df.head()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "TOOLS = \"save,pan,box_zoom,reset,wheel_zoom,tap\"\n", "\n", "source = ColumnDataSource(data=temp_df)\n", "p = figure( plot_width=800, title=\"Category wise count of crimes by year\",toolbar_location='above', tools=TOOLS)\n", "colors = brewer['Dark2'][6]\n", "\n", "p.vbar_stack(cats, x='Year', width=0.9, color=colors, source=source,\n", " legend=[value(x) for x in cats])\n", "\n", "p.y_range.start = 0\n", "p.x_range.range_padding = 0.1\n", "p.xgrid.grid_line_color = None\n", "p.axis.minor_tick_line_color = None\n", "p.outline_line_color = None\n", "p.xaxis.axis_label = 'Year'\n", "p.yaxis.axis_label = 'Total Crimes'\n", "p.legend.location = \"top_left\"\n", "p.legend.orientation = \"horizontal\"\n", "\n", "output_file(\"stacked_bar.html\", title=\"Stacked Bar Chart\")\n", "\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Heat Map\n", "The heat map shows the total number of crimes by month and year, with darker colours represnting higher number of crimes. Months in 2015 have the highest total while the least are in the some months in 2009 and 2010." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
YearMonthIncidntNum
0200514991
1200524166
2200534815
3200544711
4200555120
\n", "
" ], "text/plain": [ " Year Month IncidntNum\n", "0 2005 1 4991\n", "1 2005 2 4166\n", "2 2005 3 4815\n", "3 2005 4 4711\n", "4 2005 5 5120" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "temp_df = data.groupby(['Year', 'Month']).sum().reset_index()\n", "# temp_df['Month_Category'] = pd.concat([temp_df['Month'], temp_df['Category']], axis = 1)\n", "temp_df.head()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "TOOLS = \"hover,save,pan,box_zoom,reset,wheel_zoom,tap\"\n", "hm = figure(title=\"Month-Year wise crimes\", tools=TOOLS, toolbar_location='above')\n", "\n", "source = ColumnDataSource(temp_df)\n", "colors = brewer['BuGn'][9]\n", "colors = colors[::-1]\n", "mapper = LinearColorMapper(\n", " palette=colors, low=temp_df.IncidntNum.min(), high=temp_df.IncidntNum.max())\n", "hm.rect(x=\"Year\", y=\"Month\",width=2,height=1,source = source, \n", " fill_color={\n", " 'field': 'IncidntNum',\n", " 'transform': mapper\n", " },\n", " line_color=None)\n", "color_bar = ColorBar(\n", " color_mapper=mapper,\n", " major_label_text_font_size=\"10pt\",\n", " ticker=BasicTicker(desired_num_ticks=len(colors)),\n", " formatter=PrintfTickFormatter(),\n", " label_standoff=6,\n", " border_line_color=None,\n", " location=(0, 0))\n", "\n", "hm.add_layout(color_bar, 'right')\n", "hm.xaxis.axis_label = 'Year'\n", "hm.yaxis.axis_label = 'Month'\n", "hm.select_one(HoverTool).tooltips = [\n", " ('Year', '@Year'),('Month', '@Month'), ('Number of Crimes', '@IncidntNum')\n", "]\n", "\n", "output_file(\"heatmap.html\", title=\"Heat Map\")\n", "\n", "show(hm) # open a browser" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Multiline Plot\n", "This plot shows the distribution of crimes across categories over the years. This plot shows information similar to the stacked bar chart, except that here it is easier to note that arson and property theft amount to almost the same amount of crimes every year. Similarly, vehicle theft(except for 2005), vandalism and burglary have very similar patterns. Only larceny/theft is increasing with every passing year and its count is much higher than any of the other types of crimes." ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "TOOLS = 'crosshair,save,pan,box_zoom,reset,wheel_zoom'\n", "p = figure(title=\"Category-wise crimes through Time\", y_axis_type=\"linear\",x_axis_type='datetime', tools = TOOLS)\n", "\n", "p.line(burglary['Date'], burglary.IncidntNum, legend=\"burglary\", line_color=\"purple\", line_width = 3)\n", "p.line(stolen_property['Date'], stolen_property.IncidntNum, legend=\"stolen_property\", line_color=\"blue\", line_width = 3)\n", "\n", "p.line(vehicle_theft['Date'], vehicle_theft.IncidntNum, legend=\"vehicle_theft\", line_color = 'coral', line_width = 3)\n", "\n", "p.line(larceny['Date'], larceny.IncidntNum, legend=\"larceny\", line_color='green', line_width = 3)\n", "\n", "p.line(vandalism['Date'], vandalism.IncidntNum, legend=\"vandalism\", line_color=\"gold\", line_width = 3)\n", "\n", "p.line(arson['Date'], arson.IncidntNum, legend=\"arson\", line_color=\"magenta\",line_width = 3)\n", "\n", "p.legend.location = \"top_left\"\n", "\n", "p.xaxis.axis_label = 'Year'\n", "p.yaxis.axis_label = 'Count'\n", "\n", "output_file(\"multiline_plot.html\", title=\"Multi Line Plot\")\n", "\n", "show(p) # open a browser" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [default]", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.13" } }, "nbformat": 4, "nbformat_minor": 2 }