{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### New to Plotly?\n",
"Plotly's Python library is free and open source! [Get started](https://plot.ly/python/getting-started/) by downloading the client and [reading the primer](https://plot.ly/python/getting-started/).\n",
"
You can set up Plotly to work in [online](https://plot.ly/python/getting-started/#initialization-for-online-plotting) or [offline](https://plot.ly/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plot.ly/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": [
"#### Version Check\n",
"Plotly's python package is updated frequently. Run `pip install plotly --upgrade` to use the latest version."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'3.6.1'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly\n",
"plotly.__version__"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### U.S. Airports Map"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"import pandas as pd\n",
"\n",
"df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv')\n",
"df.head()\n",
"\n",
"df['text'] = df['airport'] + '' + df['city'] + ', ' + df['state'] + '' + 'Arrivals: ' + df['cnt'].astype(str)\n",
"\n",
"scl = [ [0,\"rgb(5, 10, 172)\"],[0.35,\"rgb(40, 60, 190)\"],[0.5,\"rgb(70, 100, 245)\"],\\\n",
" [0.6,\"rgb(90, 120, 245)\"],[0.7,\"rgb(106, 137, 247)\"],[1,\"rgb(220, 220, 220)\"] ]\n",
"\n",
"data = [ go.Scattergeo(\n",
" locationmode = 'USA-states',\n",
" lon = df['long'],\n",
" lat = df['lat'],\n",
" text = df['text'],\n",
" mode = 'markers',\n",
" marker = dict( \n",
" size = 8, \n",
" opacity = 0.8,\n",
" reversescale = True,\n",
" autocolorscale = False,\n",
" symbol = 'square',\n",
" line = dict(\n",
" width=1,\n",
" color='rgba(102, 102, 102)'\n",
" ),\n",
" colorscale = scl,\n",
" cmin = 0,\n",
" color = df['cnt'],\n",
" cmax = df['cnt'].max(),\n",
" colorbar=dict(\n",
" title=\"Incoming flights
February 2011\"\n",
" )\n",
" ))]\n",
"\n",
"layout = dict(\n",
" title = 'Most trafficked US airports
(Hover for airport names)', \n",
" geo = dict(\n",
" scope='usa',\n",
" projection=dict( type='albers usa' ),\n",
" showland = True,\n",
" landcolor = \"rgb(250, 250, 250)\",\n",
" subunitcolor = \"rgb(217, 217, 217)\",\n",
" countrycolor = \"rgb(217, 217, 217)\",\n",
" countrywidth = 0.5,\n",
" subunitwidth = 0.5 \n",
" ),\n",
" )\n",
"\n",
"fig = go.Figure(data=data, layout=layout )\n",
"py.iplot(fig, filename='d3-airports' )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### North American Precipitation Map"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"import pandas as pd\n",
"\n",
"df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2015_06_30_precipitation.csv')\n",
"\n",
"scl = [0,\"rgb(150,0,90)\"],[0.125,\"rgb(0, 0, 200)\"],[0.25,\"rgb(0, 25, 255)\"],\\\n",
"[0.375,\"rgb(0, 152, 255)\"],[0.5,\"rgb(44, 255, 150)\"],[0.625,\"rgb(151, 255, 0)\"],\\\n",
"[0.75,\"rgb(255, 234, 0)\"],[0.875,\"rgb(255, 111, 0)\"],[1,\"rgb(255, 0, 0)\"]\n",
"\n",
"data = [go.Scattergeo(\n",
" lat = df['Lat'],\n",
" lon = df['Lon'],\n",
" text = df['Globvalue'].astype(str) + ' inches',\n",
" marker = dict(\n",
" color = df['Globvalue'],\n",
" colorscale = scl,\n",
" reversescale = True,\n",
" opacity = 0.7,\n",
" size = 2, \n",
" colorbar = dict(\n",
" thickness = 10,\n",
" titleside = \"right\",\n",
" outlinecolor = \"rgba(68, 68, 68, 0)\",\n",
" ticks = \"outside\",\n",
" ticklen = 3,\n",
" showticksuffix = \"last\",\n",
" ticksuffix = \" inches\",\n",
" dtick = 0.1\n",
" ) \n",
" )\n",
")]\n",
"\n",
"layout = dict(\n",
" geo = dict(\n",
" scope = 'north america',\n",
" showland = True,\n",
" landcolor = \"rgb(212, 212, 212)\",\n",
" subunitcolor = \"rgb(255, 255, 255)\",\n",
" countrycolor = \"rgb(255, 255, 255)\",\n",
" showlakes = True,\n",
" lakecolor = \"rgb(255, 255, 255)\",\n",
" showsubunits = True,\n",
" showcountries = True,\n",
" resolution = 50,\n",
" projection = dict(\n",
" type = 'conic conformal',\n",
" rotation = dict(\n",
" lon = -100\n",
" )\n",
" ),\n",
" lonaxis = dict(\n",
" showgrid = True,\n",
" gridwidth = 0.5,\n",
" range= [ -140.0, -55.0 ],\n",
" dtick = 5\n",
" ),\n",
" lataxis = dict (\n",
" showgrid = True,\n",
" gridwidth = 0.5,\n",
" range= [ 20.0, 60.0 ],\n",
" dtick = 5\n",
" )\n",
" ),\n",
" title = 'US Precipitation 06-30-2015
Source: NOAA',\n",
")\n",
"\n",
"fig = go.Figure(data=data, layout=layout )\n",
"py.iplot(fig, filename='precipitation')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Reference\n",
"See https://plot.ly/python/reference/#scattergeo and https://plot.ly/python/reference/#layout-geo for more information and chart attribute options!"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"scrolled": true
},
"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 c:\\users\\priyat~1\\appdata\\local\\temp\\pip-req-build-s9h9i_vt\n",
"Building wheels for collected packages: publisher\n",
" Running setup.py bdist_wheel for publisher: started\n",
" Running setup.py bdist_wheel for publisher: finished with status 'done'\n",
" Stored in directory: C:\\Users\\PRIYAT~1\\AppData\\Local\\Temp\\pip-ephem-wheel-cache-6ol6d604\\wheels\\99\\3e\\a0\\fbd22ba24cca72bdbaba53dbc23c1768755fb17b3af0f33966\n",
"Successfully built publisher\n",
"Installing collected packages: publisher\n",
" Found existing installation: publisher 0.13\n",
" Uninstalling publisher-0.13:\n",
" Successfully uninstalled publisher-0.13\n",
"Successfully installed publisher-0.13\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Anaconda\\Anaconda3\\lib\\site-packages\\IPython\\nbconvert.py:13: ShimWarning:\n",
"\n",
"The `IPython.nbconvert` package has been deprecated since IPython 4.0. You should import from nbconvert instead.\n",
"\n",
"C:\\Anaconda\\Anaconda3\\lib\\site-packages\\publisher\\publisher.py:53: UserWarning:\n",
"\n",
"Did you \"Save\" this notebook before running this command? Remember to save, always save.\n",
"\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",
" 'scatter-plot-on-map.ipynb', 'python/scatter-plots-on-maps/', 'Python Scatter Plots on Maps | Examples | Plotly',\n",
" 'How to make scatter plots on maps in Python. Scatter plots on maps highlight geographic areas and can be colored by value.',\n",
" title = 'Python Scatter Plots on Maps | Plotly',\n",
" name = 'Scatter Plots on Maps',\n",
" has_thumbnail='true', thumbnail='thumbnail/scatter-plot-on-maps.jpg', \n",
" language='python',\n",
" display_as='maps', order=2,\n",
" ipynb= '~notebook_demo/57') "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"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.1"
}
},
"nbformat": 4,
"nbformat_minor": 1
}