Maps on Plotly (python) : which layer to create a satellite map with the names of the cities?

Viewed 22

I want to create a satellite map like these : https://plotly.com/python/mapbox-layers/, but i would like to display the names of the countries and cities on the map like on Google Maps (not in hover boxes like on this example, but written on the map).

Do you know how to do ?

2 Answers

Simply use the text parameter, as explained in the scatter_mapbox documentation:

import pandas as pd
us_cities = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv")

fig = px.scatter_mapbox(us_cities, lat="lat", lon="lon", text="City", hover_name="City", hover_data=["State", "Population"],
                        color_discrete_sequence=["fuchsia"], zoom=3, height=300)
fig.update_layout(mapbox_style="open-street-map")
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()

This will display the city names as shown in the map below: enter image description here

Please note that all city names will not be displayed at low zoom levels: they will gradually start appearing as you zoom in.

Obtaining a Mapbox API token will enable the display of place names on satellite maps. The Mapbox API can be obtained here. (It is free).

import plotly.express as px
import plotly.graph_objects as go
import pandas as pd

us_cities = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv")

token = open("mapbox_api_key.txt").read()

fig = px.scatter_mapbox(us_cities,
                        lat="lat",
                        lon="lon",
                        hover_name="City",
                        hover_data=["State", "Population"],
                        color_discrete_sequence=["fuchsia"],
                        zoom=3,
                        height=300
                       )
fig.update_layout(
    mapbox=dict(
    style="satellite-streets", 
    accesstoken=token,
    center=go.layout.mapbox.Center(
        lat=42.5,
        lon=-76
    ),
    pitch=0,
    zoom=5
    )
)

fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()

enter image description here

Related