溫馨提示×

Plotly怎么表添加地圖樣式和圖層

小億
93
2024-05-17 18:36:17
欄目: 編程語言

要在Plotly圖表中添加地圖樣式和圖層,您可以使用layout屬性中的mapbox對象。以下是一個例子,展示如何在Plotly圖表中添加地圖樣式和圖層:

import plotly.express as px

# 創(chuàng)建一個簡單的地圖圖表
fig = px.scatter_mapbox(df, lat="latitude", lon="longitude")

# 設置地圖樣式
fig.update_layout(
    mapbox_style="carto-positron",  # 可以選擇其他地圖樣式,如“open-street-map”、“carto-positron”、“carto-darkmatter”等
)

# 添加地圖圖層
fig.update_layout(
    mapbox_layers=[
        {
            "below": 'traces',  # 確保地圖圖層在數(shù)據(jù)圖層之下
            "sourcetype": "raster",
            "source": [
                "https://basemap.nationalmap.gov/arcgis/rest/services/USGSImageryOnly/MapServer/tile/{z}/{y}/{x}"
            ]
        }
    ]
)

fig.show()

在這個例子中,我們使用了plotly.express來創(chuàng)建一個簡單的地圖圖表,然后使用update_layout方法來設置地圖的樣式和添加地圖圖層。您可以根據(jù)需要選擇不同的地圖樣式,并根據(jù)要顯示的圖層選擇合適的mapbox_layers。

0