#!/usr/bin/env python # coding: utf-8 # #### New to Plotly? # 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/). #
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). #
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started! # #### Version Check # Plotly's Python package is updated frequently. Run `pip install plotly --upgrade` to make sure you're using the latest version. # In[1]: import plotly plotly.__version__ # #### Mapbox Access Token # # 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/. # #### Basic Example # In[2]: import plotly.plotly as py import plotly.graph_objs as go # mapbox_access_token = 'ADD_YOUR_TOKEN_HERE' data = [ go.Scattermapbox( lat=['45.5017'], lon=['-73.5673'], mode='markers', marker=go.scattermapbox.Marker( size=14 ), text=['Montreal'], ) ] layout = go.Layout( autosize=True, hovermode='closest', mapbox=go.layout.Mapbox( accesstoken=mapbox_access_token, bearing=0, center=go.layout.mapbox.Center( lat=45, lon=-73 ), pitch=0, zoom=5 ), ) fig = go.Figure(data=data, layout=layout) py.iplot(fig, filename='Montreal Mapbox') # #### Multiple Markers # In[3]: import plotly.plotly as py import plotly.graph_objs as go # mapbox_access_token = 'ADD_YOUR_TOKEN_HERE' data = [ go.Scattermapbox( lat=['38.91427','38.91538','38.91458', '38.92239','38.93222','38.90842', '38.91931','38.93260','38.91368', '38.88516','38.921894','38.93206', '38.91275'], lon=['-77.02827','-77.02013','-77.03155', '-77.04227','-77.02854','-77.02419', '-77.02518','-77.03304','-77.04509', '-76.99656','-77.042438','-77.02821', '-77.01239'], mode='markers', marker=go.scattermapbox.Marker( size=9 ), text=["The coffee bar","Bistro Bohem","Black Cat", "Snap","Columbia Heights Coffee","Azi's Cafe", "Blind Dog Cafe","Le Caprice","Filter", "Peregrine","Tryst","The Coupe", "Big Bear Cafe"], ) ] layout = go.Layout( autosize=True, hovermode='closest', mapbox=go.layout.Mapbox( accesstoken=mapbox_access_token, bearing=0, center=go.layout.mapbox.Center( lat=38.92, lon=-77.07 ), pitch=0, zoom=10 ), ) fig = go.Figure(data=data, layout=layout) py.iplot(fig, filename='Multiple Mapbox') # #### Nuclear Waste Sites on Campuses # In[4]: import plotly.plotly as py import plotly.graph_objs as go import pandas as pd # mapbox_access_token = 'ADD_YOUR_TOKEN_HERE' df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/Nuclear%20Waste%20Sites%20on%20American%20Campuses.csv') site_lat = df.lat site_lon = df.lon locations_name = df.text data = [ go.Scattermapbox( lat=site_lat, lon=site_lon, mode='markers', marker=go.scattermapbox.Marker( size=17, color='rgb(255, 0, 0)', opacity=0.7 ), text=locations_name, hoverinfo='text' ), go.Scattermapbox( lat=site_lat, lon=site_lon, mode='markers', marker=go.scattermapbox.Marker( size=8, color='rgb(242, 177, 172)', opacity=0.7 ), hoverinfo='none' )] layout = go.Layout( title='Nuclear Waste Sites on Campus', autosize=True, hovermode='closest', showlegend=False, mapbox=go.layout.Mapbox( accesstoken=mapbox_access_token, bearing=0, center=go.layout.mapbox.Center( lat=38, lon=-94 ), pitch=0, zoom=3, style='light' ), ) fig = go.Figure(data=data, layout=layout) py.iplot(fig, filename='Nuclear Waste Sites on American Campuses') # ### Dash Example # [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. # # In[5]: from IPython.display import IFrame IFrame(src= "https://dash-simple-apps.plotly.host/dash-scattermapboxplot/", width="100%", height="850px", frameBorder="0") # In[2]: from IPython.display import IFrame IFrame(src= "https://dash-simple-apps.plotly.host/dash-scattermapboxplot/code", width="100%", height=500, frameBorder="0") # #### Reference # See https://plotly.com/python/reference/#scattermapbox for more information and options! # In[1]: from IPython.display import display, HTML display(HTML('')) display(HTML('')) get_ipython().system(' pip install git+https://github.com/plotly/publisher.git --upgrade') import publisher publisher.publish( 'mapbox.ipynb', 'python/scattermapbox/', 'Python Scatter Plots with Mapbox', 'How to make scatter plots on Mapbox maps in Python.', title = 'Python Scatter Plots with Mapbox | Plotly', name = 'Scatter Plots on Mapbox', has_thumbnail='true', thumbnail='thumbnail/scatter-mapbox.jpg', language='python', page_type='example_index', ipynb='~notebook_demo/261', display_as='maps', order=7, mapbox_access_token = 'your access token' ) # In[ ]: