Show Sidebar Hide Sidebar

Lines on Maps in Python

How to draw lines, great circles, and contours on 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 use the latest version.

In [1]:
import plotly
plotly.__version__
Out[1]:
'3.6.1'

US Flight Paths Map

In [2]:
import plotly.plotly as py
import plotly.graph_objs as go

import pandas as pd

df_airports = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv')
df_airports.head()

df_flight_paths = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_aa_flight_paths.csv')
df_flight_paths.head()

airports = [go.Scattergeo(
    locationmode = 'USA-states',
    lon = df_airports['long'],
    lat = df_airports['lat'],
    hoverinfo = 'text',
    text = df_airports['airport'],
    mode = 'markers',
    marker = go.scattergeo.Marker(
        size = 2,
        color = 'rgb(255, 0, 0)',
        line = go.scattergeo.marker.Line(
            width = 3,
            color = 'rgba(68, 68, 68, 0)'
        )
    ))]

flight_paths = []
for i in range(len(df_flight_paths)):
    flight_paths.append(
        go.Scattergeo(
            locationmode = 'USA-states',
            lon = [df_flight_paths['start_lon'][i], df_flight_paths['end_lon'][i]],
            lat = [df_flight_paths['start_lat'][i], df_flight_paths['end_lat'][i]],
            mode = 'lines',
            line = go.scattergeo.Line(
                width = 1,
                color = 'red',
            ),
            opacity = float(df_flight_paths['cnt'][i]) / float(df_flight_paths['cnt'].max()),
        )
    )

layout = go.Layout(
    title = go.layout.Title(
        text = 'Feb. 2011 American Airline flight paths<br>(Hover for airport names)'
    ),
    showlegend = False,
    geo = go.layout.Geo(
        scope = 'north america',
        projection = go.layout.geo.Projection(type = 'azimuthal equal area'),
        showland = True,
        landcolor = 'rgb(243, 243, 243)',
        countrycolor = 'rgb(204, 204, 204)',
    ),
)

fig = go.Figure(data = flight_paths + airports, layout = layout)
py.iplot(fig, filename = 'd3-flight-paths')
Out[2]:

London to NYC Great Circle

In [3]:
import plotly.plotly as py
import plotly.graph_objs as go

nyc_london = [go.Scattergeo(
    lat = [40.7127, 51.5072],
    lon = [-74.0059, 0.1275],
    mode = 'lines',
    line = go.scattergeo.Line(
        width = 2,
        color = 'blue',
    ),
)]

layout = go.Layout(
    title = go.layout.Title(
        text = 'London to NYC Great Circle'
    ),
    showlegend = False,
    geo = go.layout.Geo(
        resolution = 50,
        showland = True,
        showlakes = True,
        landcolor = 'rgb(204, 204, 204)',
        countrycolor = 'rgb(204, 204, 204)',
        lakecolor = 'rgb(255, 255, 255)',
        projection = go.layout.geo.Projection(
            type = "equirectangular"
        ),
        coastlinewidth = 2,
        lataxis = go.layout.geo.Lataxis(
            range = [20, 60],
            showgrid = True,
            dtick = 10
        ),
        lonaxis = go.layout.geo.Lonaxis(
            range = [-100, 20],
            showgrid = True,
            dtick = 20
        ),
    )
)

fig = go.Figure(data = nyc_london, layout = layout)
py.iplot(fig, filename = 'd3-great-circle')
Out[3]:

Contour lines on globe

In [4]:
import plotly.plotly as py
import plotly.graph_objs as go

import pandas as pd
try:
    from itertools import izip# Python 2
except ImportError:
    izip = zip# Python 3

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/globe_contours.csv')
df.head()

contours = []

scl = ['rgb(213,62,79)', 'rgb(244,109,67)', 'rgb(253,174,97)', \
    'rgb(254,224,139)', 'rgb(255,255,191)', 'rgb(230,245,152)', \
    'rgb(171,221,164)', 'rgb(102,194,165)', 'rgb(50,136,189)'
]

def pairwise(iterable):
    a = iter(iterable)
    return izip(a, a)

i = 0
for lat, lon in pairwise(df.columns):
    contours.append(go.Scattergeo(
        lon = df[lon],
        lat = df[lat],
        mode = 'lines',
        line = go.scattergeo.Line(
            width = 2,
            color = scl[i]
        )))
    i = 0 if i + 1 >= len(df.columns) / 4 else i + 1

layout = go.Layout(
    title = go.layout.Title(
        text = 'Contour lines over globe<br>(Click and drag to rotate)'
    ),
    showlegend = False,
    geo = go.layout.Geo(
        showland = True,
        showlakes = True,
        showcountries = True,
        showocean = True,
        countrywidth = 0.5,
        landcolor = 'rgb(230, 145, 56)',
        lakecolor = 'rgb(0, 255, 255)',
        oceancolor = 'rgb(0, 255, 255)',
        projection = go.layout.geo.Projection(
            type = 'orthographic',
            rotation = go.layout.geo.projection.Rotation(
                lon = -100,
                lat = 40,
                roll = 0
            )
        ),
        lonaxis = go.layout.geo.Lonaxis(
            showgrid = True,
            gridcolor = 'rgb(102, 102, 102)',
            gridwidth = 0.5
        ),
        lataxis = go.layout.geo.Lataxis(
            showgrid = True,
            gridcolor = 'rgb(102, 102, 102)',
            gridwidth = 0.5
        )
    )
)

fig = go.Figure(data = contours, layout = layout)
py.iplot(fig, filename = 'd3-globe')
Out[4]:

Reference

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