Skip to content

Latest commit

 

History

History
93 lines (74 loc) · 2.71 KB

lines-on-mapbox.md

File metadata and controls

93 lines (74 loc) · 2.71 KB
jupyter
jupytext kernelspec language_info plotly
notebook_metadata_filter text_representation
all
extension format_name format_version jupytext_version
.md
markdown
1.1
1.1.1
display_name language name
Python 3
python
python3
codemirror_mode file_extension mimetype name nbconvert_exporter pygments_lexer version
name version
ipython
3
.py
text/x-python
python
python
ipython3
3.6.8
description display_as language layout name order page_type permalink thumbnail
How to draw a line on Map in Python with Plotly.
maps
python
base
Lines on Mapbox
2
example_index
python/lines-on-mapbox/
thumbnail/line_mapbox.jpg

Mapbox Access Token and Base Map Configuration

To plot on Mapbox maps with Plotly you may need a Mapbox account and a public Mapbox Access Token. See our Mapbox Map Layers documentation for more information.

To draw a line on your map, you either can use px.line_mapbox() in Plotly Express, or Scattermapbox traces. Below we show you how to draw a line on Mapbox using Plotly Express.

Lines on Mapbox maps using Plotly Express

import pandas as pd

us_cities = pd.read_csv("https://raw-hub.myxuebi.top/plotly/datasets/master/us-cities-top-1k.csv")
us_cities = us_cities.query("State in ['New York', 'Ohio']")

import plotly.express as px

fig = px.line_mapbox(us_cities, lat="lat", lon="lon", color="State", zoom=3, height=300)

fig.update_layout(mapbox_style="stamen-terrain", mapbox_zoom=4, mapbox_center_lat = 41,
    margin={"r":0,"t":0,"l":0,"b":0})

fig.show()

Lines on Mapbox maps using Scattermapbox traces

This example uses go.Scattermapbox and sets the mode attribute to a combination of markers and line.

import plotly.graph_objects as go

fig = go.Figure(go.Scattermapbox(
    mode = "markers+lines",
    lon = [10, 20, 30],
    lat = [10, 20,30],
    marker = {'size': 10}))

fig.add_trace(go.Scattermapbox(
    mode = "markers+lines",
    lon = [-50, -60,40],
    lat = [30, 10, -20],
    marker = {'size': 10}))

fig.update_layout(
    margin ={'l':0,'t':0,'b':0,'r':0},
    mapbox = {
        'center': {'lon': 10, 'lat': 10},
        'style': "stamen-terrain",
        'center': {'lon': -20, 'lat': -20},
        'zoom': 1})

fig.show()

Reference

See https://plotly.com/python/reference/#scattermapbox for more information about mapbox and their attribute options.