Show Sidebar Hide Sidebar

Scatter Plots on Mapbox in Python

How to make scatter plots on Mapbox maps in Python.

New to Plotly?

Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (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__
Out[1]:
'2.0.6'

Mapbox Access Token

To plot on Mapbox maps with Plotly you'll need a Mapbox account and a Mapbox Access Token which you can add to your Plotly settings. If you're using a Plotly On-Premise server, please see additional instructions here: https://help.plot.ly/mapbox-atlas/.

Basic Example

In [1]:
import plotly.plotly as py
from plotly.graph_objs import *

mapbox_access_token = 'ADD_YOUR_TOKEN_HERE'

data = Data([
    Scattermapbox(
        lat=['45.5017'],
        lon=['-73.5673'],
        mode='markers',
        marker=Marker(
            size=14
        ),
        text=['Montreal'],
    )
])

layout = Layout(
    autosize=True,
    hovermode='closest',
    mapbox=dict(
        accesstoken=mapbox_access_token,
        bearing=0,
        center=dict(
            lat=45,
            lon=-73
        ),
        pitch=0,
        zoom=5
    ),
)

fig = dict(data=data, layout=layout)
py.iplot(fig, filename='Montreal Mapbox')
Out[1]:

Multiple Markers

In [2]:
import plotly.plotly as py
from plotly.graph_objs import *

mapbox_access_token = 'ADD_YOUR_TOKEN_HERE'

data = Data([
    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=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 = Layout(
    autosize=True,
    hovermode='closest',
    mapbox=dict(
        accesstoken=mapbox_access_token,
        bearing=0,
        center=dict(
            lat=38.92,
            lon=-77.07
        ),
        pitch=0,
        zoom=10
    ),
)

fig = dict(data=data, layout=layout)
py.iplot(fig, filename='Multiple Mapbox')
Out[2]:

Nuclear Waste Sites on Campuses

In [3]:
import plotly.plotly as py
from plotly.graph_objs import *

import pandas as pd

mapbox_access_token = 'ADD_YOUR_TOKEN_HERE'

json_data = pd.read_json('https://raw.githubusercontent.com/ottlngr/2016-15/ottlngr/ottlngr/sites.json')
rating_one_site_lat = []
rating_one_site_lon = []
locations_name = []

for index in range(len(json_data)):
    if json_data['locations'][index] != []:
        if json_data['site'][index]['rating'] == 1:
            rating_one_site_lat.append(json_data['locations'][index][0]['latitude'])
            rating_one_site_lon.append(json_data['locations'][index][0]['longitude'])
            locations_name.append(json_data['site'][index]['name'])

data = Data([
    Scattermapbox(
        lat=rating_one_site_lat,
        lon=rating_one_site_lon,
        mode='markers',
        marker=Marker(
            size=17,
            color='rgb(255, 0, 0)',
            opacity=0.7
        ),
        text=locations_name,
        hoverinfo='text'
    ),
    Scattermapbox(
        lat=rating_one_site_lat,
        lon=rating_one_site_lon,
        mode='markers',
        marker=Marker(
            size=8,
            color='rgb(242, 177, 172)',
            opacity=0.7
        ),
        hoverinfo='skip'
    )]
)
        
layout = Layout(
    title='Nuclear Waste Sites on Campus',
    autosize=True,
    hovermode='closest',
    showlegend=False,
    mapbox=dict(
        accesstoken=mapbox_access_token,
        bearing=0,
        center=dict(
            lat=38,
            lon=-94
        ),
        pitch=0,
        zoom=3,
        style='light'
    ),
)

fig = dict(data=data, layout=layout)
py.iplot(fig, filename='Nuclear Waste Sites on American Campuses')
Out[3]:

All Nuclear Waste Sites and their Current Status

In [4]:
import plotly.plotly as py
from plotly.graph_objs import *

import pandas as pd

mapbox_access_token = 'ADD_YOUR_TOKEN_HERE'

json_data = pd.read_json('https://raw.githubusercontent.com/ottlngr/2016-15/ottlngr/ottlngr/sites.json')
site_lat = []
site_lon = []
locations_name = []
colors = []

for index in range(len(json_data)):
    if json_data['locations'][index] != []:
        site_lat.append(json_data['locations'][index][0]['latitude'])
        site_lon.append(json_data['locations'][index][0]['longitude'])
        locations_name.append(json_data['site'][index]['name'])
        # add colors to list depending on 'rating' value
        if json_data['site'][index]['rating'] == 0:
            colors.append('rgb(247, 200, 108)')
        elif json_data['site'][index]['rating'] == 1:
            colors.append('rgb(79, 161, 69)')
        elif json_data['site'][index]['rating'] == 2:
            colors.append('rgb(255, 0, 0)')
        else:
            colors.append('rgb(0, 0, 0)')

data = Data([
    Scattermapbox(
        lat=site_lat,
        lon=site_lon,
        mode='markers',
        marker=Marker(
            size=17,
            color=colors,
            opacity=0.7
        ),
        text=locations_name,
        hoverinfo='text',
        showlegend=False
    ),
    Scattermapbox(
        lat=site_lat,
        lon=site_lon,
        mode='markers',
        marker=Marker(
            size=8,
            color='rgb(242, 177, 172)',
            opacity=0.7
        ),
        hoverinfo='skip',
        showlegend=False
    )
])
        
layout = Layout(
    title='All Nuclear Waste Sites and their Current Status',
    autosize=True,
    hovermode='closest',
    mapbox=dict(
        accesstoken=mapbox_access_token,
        bearing=0,
        center=dict(
            lat=38,
            lon=-94
        ),
        pitch=0,
        zoom=3,
        style='light'
    )
)

fig = dict(data=data, layout=layout)
py.iplot(fig, filename='All Nuclear Waste Sites and their Current Status')
Out[4]:

Reference

See https://plot.ly/python/reference/#scattermapbox for more information and options!

Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.