{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### New to Plotly?\n",
"Plotly's Python library is free and open source! [Get started](https://plotly.com/python/getting-started/) by downloading the client and [reading the primer](https://plotly.com/python/getting-started/).\n",
"
You can set up Plotly to work in [online](https://plotly.com/python/getting-started/#initialization-for-online-plotting) or [offline](https://plotly.com/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plotly.com/python/getting-started/#start-plotting-online).\n",
"
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### US map small multiples"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import pandas as pd\n",
"df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/1962_2006_walmart_store_openings.csv')\n",
"df.head()\n",
"\n",
"data = []\n",
"layout = dict(\n",
" title = 'New Walmart Stores per year 1962-2006
\\\n",
"Source: \\\n",
"University of Minnesota',\n",
" # showlegend = False,\n",
" autosize = False,\n",
" width = 1000,\n",
" height = 900,\n",
" hovermode = False,\n",
" legend = dict(\n",
" x=0.7,\n",
" y=-0.1,\n",
" bgcolor=\"rgba(255, 255, 255, 0)\",\n",
" font = dict( size=11 ),\n",
" )\n",
")\n",
"years = df['YEAR'].unique()\n",
"\n",
"for i in range(len(years)):\n",
" geo_key = 'geo'+str(i+1) if i != 0 else 'geo'\n",
" lons = list(df[ df['YEAR'] == years[i] ]['LON'])\n",
" lats = list(df[ df['YEAR'] == years[i] ]['LAT'])\n",
" # Walmart store data\n",
" data.append(\n",
" dict(\n",
" type = 'scattergeo',\n",
" showlegend=False,\n",
" lon = lons,\n",
" lat = lats,\n",
" geo = geo_key,\n",
" name = years[i],\n",
" marker = dict(\n",
" color = \"rgb(0, 0, 255)\",\n",
" opacity = 0.5\n",
" )\n",
" )\n",
" )\n",
" # Year markers\n",
" data.append(\n",
" dict(\n",
" type = 'scattergeo',\n",
" showlegend = False,\n",
" lon = [-78],\n",
" lat = [47],\n",
" geo = geo_key,\n",
" text = [years[i]],\n",
" mode = 'text',\n",
" )\n",
" )\n",
" layout[geo_key] = dict(\n",
" scope = 'usa',\n",
" showland = True,\n",
" landcolor = 'rgb(229, 229, 229)',\n",
" showcountries = False,\n",
" domain = dict( x = [], y = [] ),\n",
" subunitcolor = \"rgb(255, 255, 255)\",\n",
" )\n",
"\n",
"\n",
"def draw_sparkline( domain, lataxis, lonaxis ):\n",
" ''' Returns a sparkline layout object for geo coordinates '''\n",
" return dict(\n",
" showland = False,\n",
" showframe = False,\n",
" showcountries = False,\n",
" showcoastlines = False,\n",
" domain = domain,\n",
" lataxis = lataxis,\n",
" lonaxis = lonaxis,\n",
" bgcolor = 'rgba(255,200,200,0.0)'\n",
" )\n",
"\n",
"# Stores per year sparkline\n",
"layout['geo44'] = draw_sparkline({'x':[0.6,0.8], 'y':[0,0.15]}, \\\n",
" {'range':[-5.0, 30.0]}, {'range':[0.0, 40.0]} )\n",
"data.append(\n",
" dict(\n",
" type = 'scattergeo',\n",
" mode = 'lines',\n",
" lat = list(df.groupby(by=['YEAR']).count()['storenum']/1e1),\n",
" lon = range(len(df.groupby(by=['YEAR']).count()['storenum']/1e1)),\n",
" line = dict( color = \"rgb(0, 0, 255)\" ),\n",
" name = \"New stores per year
Peak of 178 stores per year in 1990\",\n",
" geo = 'geo44',\n",
" )\n",
")\n",
"\n",
"# Cumulative sum sparkline\n",
"layout['geo45'] = draw_sparkline({'x':[0.8,1], 'y':[0,0.15]}, \\\n",
" {'range':[-5.0, 50.0]}, {'range':[0.0, 50.0]} )\n",
"data.append(\n",
" dict(\n",
" type = 'scattergeo',\n",
" mode = 'lines',\n",
" lat = list(df.groupby(by=['YEAR']).count().cumsum()['storenum']/1e2),\n",
" lon = range(len(df.groupby(by=['YEAR']).count()['storenum']/1e1)),\n",
" line = dict( color = \"rgb(214, 39, 40)\" ),\n",
" name =\"Cumulative sum
3176 stores total in 2006\",\n",
" geo = 'geo45',\n",
" )\n",
")\n",
"\n",
"z = 0\n",
"COLS = 5\n",
"ROWS = 9\n",
"for y in reversed(range(ROWS)):\n",
" for x in range(COLS):\n",
" geo_key = 'geo'+str(z+1) if z != 0 else 'geo'\n",
" layout[geo_key]['domain']['x'] = [float(x)/float(COLS), float(x+1)/float(COLS)]\n",
" layout[geo_key]['domain']['y'] = [float(y)/float(ROWS), float(y+1)/float(ROWS)]\n",
" z=z+1\n",
" if z > 42:\n",
" break\n",
"\n",
"fig = { 'data':data, 'layout':layout }\n",
"py.iplot( fig, filename='US Walmart growth', height=900, width=1000 )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Reference\n",
"See https://plotly.com/python/reference/#scattergeo for more information and chart attribute options!"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting git+https://github.com/plotly/publisher.git\n",
" Cloning https://github.com/plotly/publisher.git to /var/folders/ld/6cl3s_l50wd40tdjq2b03jxh0000gp/T/pip-3nPpfj-build\n",
"Installing collected packages: publisher\n",
" Found existing installation: publisher 0.10\n",
" Uninstalling publisher-0.10:\n",
" Successfully uninstalled publisher-0.10\n",
" Running setup.py install for publisher ... \u001b[?25l-\b \b\\\b \bdone\n",
"\u001b[?25hSuccessfully installed publisher-0.10\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/brandendunbar/Desktop/test/venv/lib/python2.7/site-packages/IPython/nbconvert.py:13: ShimWarning: The `IPython.nbconvert` package has been deprecated. You should import from nbconvert instead.\n",
" \"You should import from nbconvert instead.\", ShimWarning)\n",
"/Users/brandendunbar/Desktop/test/venv/lib/python2.7/site-packages/publisher/publisher.py:53: UserWarning: Did you \"Save\" this notebook before running this command? Remember to save, always save.\n",
" warnings.warn('Did you \"Save\" this notebook before running this command? '\n"
]
}
],
"source": [
"from IPython.display import display, HTML\n",
"\n",
"display(HTML(''))\n",
"display(HTML(''))\n",
"\n",
"! pip install git+https://github.com/plotly/publisher.git --upgrade\n",
"import publisher\n",
"publisher.publish(\n",
" 'map-subplots.ipynb', ' python/map-subplots-and-small-multiples/', ' Python Map Subplots and Map Small Multiples| Plotly',\n",
" 'How to make map subplots and map small multiples in Python.',\n",
" title = 'Python Map Subplots and Map Small Multiples | plotly',\n",
" name = 'Map Subplots',\n",
" has_thumbnail='true', thumbnail='thumbnail/map-subplots.jpg', \n",
" language='python', page_type='example_index'\n",
" display_as='multiple_axes', order=5,\n",
" ipynb= '~notebook_demo/59') "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"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.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}