Map Configuration and Styling on Geo Maps in Python

How to configure and style base maps for outline-based Geo Maps.


Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Try Plotly Studio now.

Tile Maps vs Outline Maps

Plotly supports two different kinds of maps:

If your figure is created with a px.scatter_map, px.scatter_mapbox, px.line_map, px.line_mapbox, px.choropleth_map, px.choropleth_mapbox, px.density_map, or px.density_mapbox function or otherwise contains one or more traces of type go.Scattermap, go.Scattermapbox, go.Choroplethmap, go.Choroplethmapbox, go.Densitymap, or go.Densitymapbox, the layout.map object in your figure contains configuration information for the map itself.

  • Outline-based maps

Geo maps are outline-based maps. If your figure is created with a px.scatter_geo, px.line_geo or px.choropleth function or otherwise contains one or more traces of type go.Scattergeo or go.Choropleth, the layout.geo object in your figure contains configuration information for the map itself.

This page documents Geo outline-based maps, and the Tile Map Layers documentation describes how to configure tile-based maps.

Note: Plotly Express cannot create empty figures, so the examples below mostly create an "empty" map using fig = go.Figure(go.Scattergeo()). That said, every configuration option here is equally applicable to non-empty maps created with the Plotly Express px.scatter_geo, px.line_geo or px.choropleth functions.

Physical Base Maps

Plotly Geo maps have a built-in base map layer composed of physical and cultural (i.e. administrative border) data.

In Plotly.py 6.3 and later, the base map layer is created from the following sources:

  • UN data for country borders, coastlines, land, and oceans layers.
  • Natural Earth data for lakes, rivers, and subunits layers.

In earlier versions of Plotly.py, the base map layer is based on Natural Earth data only. Plotly includes data from Natural Earth "as-is". This dataset draws boundaries of countries according to de facto status. See the Natural Earth page for more details.

Various lines and area fills can be shown or hidden, and their color and line-widths specified. In the default plotly template, a map frame and physical features such as a coastal outline and filled land areas are shown, at a small-scale 1:110m resolution:

In [1]:
import plotly.graph_objects as go

fig = go.Figure(go.Scattergeo())
fig.update_layout(height=300, margin={"r":0,"t":0,"l":0,"b":0})
fig.show()

Here is a map with all physical features enabled and styled, at a larger-scale 1:50m resolution:

In [2]:
import plotly.graph_objects as go

fig = go.Figure(go.Scattergeo())
fig.update_geos(
    resolution=50,
    showcoastlines=True, coastlinecolor="RebeccaPurple",
    showland=True, landcolor="LightGreen",
    showocean=True, oceancolor="LightBlue",
    showlakes=True, lakecolor="Blue",
    showrivers=True, rivercolor="Blue"
)
fig.update_layout(height=300, margin={"r":0,"t":0,"l":0,"b":0})
fig.show()

Disabling Base Maps

In certain cases, such as large scale choropleth maps, the default physical map can be distracting. In this case the layout.geo.visible attribute can be set to False to hide all base map attributes except those which are explicitly set to true. For example in the following map we hide all physical features except rivers and lakes, neither of which are shown by default:

In [3]:
import plotly.graph_objects as go

fig = go.Figure(go.Scattergeo())
fig.update_geos(
    visible=False,
    resolution=50,
    showlakes=True, lakecolor="Blue",
    showrivers=True, rivercolor="Blue"
)
fig.update_layout(height=300, margin={"r":0,"t":0,"l":0,"b":0})

fig.show()

Cultural Base Maps

In addition to physical base map features, a "cultural" base map is included which is composed of country borders and selected sub-country borders such as states.

In Plotly.py 6.3 and later, this base map is created from UN data for country borders, and Natural Earth data for sub-country borders.

In earlier versions of Plotly.py, this base map is based on Natural Earth data only. Plotly includes data from Natural Earth "as-is". This dataset draws boundaries of countries according to defacto status. See the Natural Earth page for more details.

To create a map with your own cultural features please refer to our choropleth documentation.

Here is a map with only cultural features enabled and styled, at a 1:50m resolution, which includes only country boundaries. See below for country sub-unit cultural base map features:

In [4]:
import plotly.graph_objects as go

fig = go.Figure(go.Scattergeo())
fig.update_geos(
    visible=False, resolution=50,
    showcountries=True, countrycolor="RebeccaPurple"
)
fig.update_layout(height=300, margin={"r":0,"t":0,"l":0,"b":0})
fig.show()

Map Projections

Geo maps are drawn according to a given map projection that flattens the Earth's roughly-spherical surface into a 2-dimensional space. In the following examples, we show the 'orthographic' and 'natural earth' projections, two of the many projection types available. For a full list of available projection types, see the layout.geo reference documentation.

In [5]:
import plotly.graph_objects as go

fig = go.Figure(go.Scattergeo())
fig.update_geos(projection_type="orthographic")
fig.update_layout(height=300, margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
In [6]:
import plotly.graph_objects as go

fig = go.Figure(go.Scattergeo())
fig.update_geos(projection_type="natural earth")
fig.update_layout(height=300, margin={"r":0,"t":0,"l":0,"b":0})
fig.show()