{
"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": [
"#### Version Check\n",
"Plotly's Python package is updated frequently. Run `pip install plotly --upgrade` to make sure you're using 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": [
"#### Mapbox Access Token\n",
"\n",
"To plot on Mapbox maps with Plotly you'll need a Mapbox account and a public [Mapbox Access Token](https://www.mapbox.com/studio) which you can add to your [Plotly settings](https://plotly.com/settings/mapbox). If you're using a Chart Studio Enterprise server, please see additional instructions here: https://help.plot.ly/mapbox-atlas/. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Basic Example"
]
},
{
"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",
"# mapbox_access_token = 'ADD_YOUR_TOKEN_HERE'\n",
"\n",
"data = [\n",
" go.Scattermapbox(\n",
" lat=['45.5017'],\n",
" lon=['-73.5673'],\n",
" mode='markers',\n",
" marker=go.scattermapbox.Marker(\n",
" size=14\n",
" ),\n",
" text=['Montreal'],\n",
" )\n",
"]\n",
"\n",
"layout = go.Layout(\n",
" autosize=True,\n",
" hovermode='closest',\n",
" mapbox=go.layout.Mapbox(\n",
" accesstoken=mapbox_access_token,\n",
" bearing=0,\n",
" center=go.layout.mapbox.Center(\n",
" lat=45,\n",
" lon=-73\n",
" ),\n",
" pitch=0,\n",
" zoom=5\n",
" ),\n",
")\n",
"\n",
"fig = go.Figure(data=data, layout=layout)\n",
"py.iplot(fig, filename='Montreal Mapbox')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Multiple Markers"
]
},
{
"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",
"# mapbox_access_token = 'ADD_YOUR_TOKEN_HERE'\n",
"\n",
"data = [\n",
" go.Scattermapbox(\n",
" lat=['38.91427','38.91538','38.91458',\n",
" '38.92239','38.93222','38.90842',\n",
" '38.91931','38.93260','38.91368',\n",
" '38.88516','38.921894','38.93206',\n",
" '38.91275'],\n",
" lon=['-77.02827','-77.02013','-77.03155',\n",
" '-77.04227','-77.02854','-77.02419',\n",
" '-77.02518','-77.03304','-77.04509',\n",
" '-76.99656','-77.042438','-77.02821',\n",
" '-77.01239'],\n",
" mode='markers',\n",
" marker=go.scattermapbox.Marker(\n",
" size=9\n",
" ),\n",
" text=[\"The coffee bar\",\"Bistro Bohem\",\"Black Cat\",\n",
" \"Snap\",\"Columbia Heights Coffee\",\"Azi's Cafe\",\n",
" \"Blind Dog Cafe\",\"Le Caprice\",\"Filter\",\n",
" \"Peregrine\",\"Tryst\",\"The Coupe\",\n",
" \"Big Bear Cafe\"],\n",
" )\n",
"]\n",
"\n",
"layout = go.Layout(\n",
" autosize=True,\n",
" hovermode='closest',\n",
" mapbox=go.layout.Mapbox(\n",
" accesstoken=mapbox_access_token,\n",
" bearing=0,\n",
" center=go.layout.mapbox.Center(\n",
" lat=38.92,\n",
" lon=-77.07\n",
" ),\n",
" pitch=0,\n",
" zoom=10\n",
" ),\n",
")\n",
"\n",
"fig = go.Figure(data=data, layout=layout)\n",
"py.iplot(fig, filename='Multiple Mapbox')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Nuclear Waste Sites on Campuses"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 4,
"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",
"# mapbox_access_token = 'ADD_YOUR_TOKEN_HERE'\n",
"\n",
"df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/Nuclear%20Waste%20Sites%20on%20American%20Campuses.csv')\n",
"site_lat = df.lat\n",
"site_lon = df.lon\n",
"locations_name = df.text\n",
"\n",
"data = [\n",
" go.Scattermapbox(\n",
" lat=site_lat,\n",
" lon=site_lon,\n",
" mode='markers',\n",
" marker=go.scattermapbox.Marker(\n",
" size=17,\n",
" color='rgb(255, 0, 0)',\n",
" opacity=0.7\n",
" ),\n",
" text=locations_name,\n",
" hoverinfo='text'\n",
" ),\n",
" go.Scattermapbox(\n",
" lat=site_lat,\n",
" lon=site_lon,\n",
" mode='markers',\n",
" marker=go.scattermapbox.Marker(\n",
" size=8,\n",
" color='rgb(242, 177, 172)',\n",
" opacity=0.7\n",
" ),\n",
" hoverinfo='none'\n",
" )]\n",
" \n",
"layout = go.Layout(\n",
" title='Nuclear Waste Sites on Campus',\n",
" autosize=True,\n",
" hovermode='closest',\n",
" showlegend=False,\n",
" mapbox=go.layout.Mapbox(\n",
" accesstoken=mapbox_access_token,\n",
" bearing=0,\n",
" center=go.layout.mapbox.Center(\n",
" lat=38,\n",
" lon=-94\n",
" ),\n",
" pitch=0,\n",
" zoom=3,\n",
" style='light'\n",
" ),\n",
")\n",
"\n",
"fig = go.Figure(data=data, layout=layout)\n",
"py.iplot(fig, filename='Nuclear Waste Sites on American Campuses')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dash Example"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Dash](https://plotly.com/products/dash/) is an Open Source Python library which can help you convert plotly figures into a reactive, web-based application. Below is a simple example of a dashboard created using Dash. Its [source code](https://github.com/plotly/simple-example-chart-apps/tree/master/dash-scattermapboxplot) can easily be deployed to a PaaS.\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.display import IFrame\n",
"IFrame(src= \"https://dash-simple-apps.plotly.host/dash-scattermapboxplot/\", width=\"100%\", height=\"850px\", frameBorder=\"0\")\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.display import IFrame\n",
"IFrame(src= \"https://dash-simple-apps.plotly.host/dash-scattermapboxplot/code\", width=\"100%\", height=500, frameBorder=\"0\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Reference\n",
"See https://plotly.com/python/reference/#scattermapbox for more information and options!"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"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",
" 'mapbox.ipynb', 'python/scattermapbox/', 'Python Scatter Plots with Mapbox',\n",
" 'How to make scatter plots on Mapbox maps in Python.',\n",
" title = 'Python Scatter Plots with Mapbox | Plotly',\n",
" name = 'Scatter Plots on Mapbox',\n",
" has_thumbnail='true', thumbnail='thumbnail/scatter-mapbox.jpg', \n",
" language='python', page_type='example_index', ipynb='~notebook_demo/261',\n",
" display_as='maps', order=7, mapbox_access_token = 'your access token'\n",
")"
]
},
{
"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.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 1
}