简体   繁体   English

Plotly (python) 上的地图:在哪一层创建带有城市名称的卫星 map?

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

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). 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就像这个例子,但写在地图上)。

Do you know how to do?你知道该怎么做吗?

Simply use the text parameter, as explained in the scatter_mapbox documentation :只需使用text参数,如scatter_mapbox 文档中所述

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:这将显示城市名称,如下面的 map 所示: 在此处输入图像描述

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.获取 Mapbox API 令牌将启用在卫星地图上显示地名。 The Mapbox API can be obtained here . Mapbox API 可以在这里获得。 (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()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM