如何使用带有 python plotly 库的等值线图函数按大陆可视化?

How to use choropleth map function with python plotly library to visualize by continent?

我正在尝试创建一个等值线图来从数据框中可视化每个大陆的总页面浏览量,但是 plotly 库函数的 locationmode 参数没有 it.Is 有什么办法可以解决这个问题吗?当我使用 'country names' 作为位置模式时,它不起作用。

st.subheader("Continent's Total Pageviews Choropleth Map")
fig2 = go.Figure(data= go.Choropleth(
locations= df1['continent'],
z = df1['total_pageviews'].astype(float),
locationmode = 'country names',
colorscale = 'Reds',
colorbar_title = "Total Pageviews",
))
fig2.update_layout( width = 1100 , height = 500 )
st.write(fig2)
  • 获取各​​大洲的 GEOJSON
  • 建立一个geopandas数据框,整合页面浏览量数据
  • 简单mapbox绘图
import requests
import geopandas as gpd
import numpy as np
import plotly.express as px

cont = requests.get(
    "https://gist.githubusercontent.com/hrbrmstr/91ea5cc9474286c72838/raw/59421ff9b268ff0929b051ddafafbeb94a4c1910/continents.json"
)
gdf = gpd.GeoDataFrame.from_features(cont.json())

gdf = gdf.assign(
    total_pageviews=np.random.randint(10 ** 7, 10 ** 9, len(gdf))
).set_index("CONTINENT")

px.choropleth_mapbox(
    gdf,
    geojson=gdf.geometry,
    locations=gdf.index,
    color="total_pageviews",
    mapbox_style="carto-positron",
    color_continuous_scale="Reds",
    opacity=0.5,
    zoom=1,
).update_layout(margin={"l": 0, "r": 0, "b": 0, "t": 0})