您的图像未在 SO 中显示。你想要的代码:
# restrict to just India
fig_choropleth.update_geos(fitbounds="locations", visible=False)
您已经注意到如何确保显示所有状态。这可以通过构建基本轨迹然后添加状态子集作为 Choropleth 来完成
带有印度州 geojson 和模拟数据框的完整 MWE
import geopandas as gpd
import pandas as pd
import numpy as np
import plotly.express as px
# get some geojson for India. Reduce somplexity of geomtry to make it more efficient
url = "https://raw.githubusercontent.com/Subhash9325/GeoJson-Data-of-Indian-States/master/Indian_States"
gdf = gpd.read_file(url)
gdf["geometry"] = gdf.to_crs(gdf.estimate_utm_crs()).simplify(1000).to_crs(gdf.crs)
india_states = gdf.rename(columns={"NAME_1": "ST_NM"}).__geo_interface__
# simulate data frame
dff = pd.DataFrame(
{
"state": ['Andaman and Nicobar', 'Andhra Pradesh', 'Arunachal Pradesh', 'Assam', 'Bihar', 'Chandigarh', 'Chhattisgarh', 'Dadra and Nagar Haveli', 'Daman and Diu', 'Delhi', 'Goa', 'Gujarat', 'Haryana', 'Himachal Pradesh', 'Jammu and Kashmir', 'Jharkhand', 'Karnataka', 'Kerala', 'Lakshadweep', 'Madhya Pradesh', 'Maharashtra', 'Manipur', 'Meghalaya', 'Mizoram', 'Nagaland', 'Orissa', 'Puducherry', 'Punjab', 'Rajasthan', 'Sikkim', 'Tamil Nadu', 'Tripura', 'Uttar Pradesh', 'Uttaranchal', 'West Bengal'], # fmt: skip
"content_view": np.random.randint(1, 5, 35),
}
)
# data frame only has a subset of states...
dff = dff.sample(20)
# create base map of all India states
fig_choropleth = px.choropleth(
pd.json_normalize(india_states["features"])["properties.ST_NM"],
locations="properties.ST_NM",
geojson=india_states,
featureidkey="properties.ST_NM",
color_discrete_sequence=["lightgrey"],
)
# users code to generate choropleth
fig_choropleth.add_traces(
px.choropleth(
dff,
locations="state",
geojson=india_states,
featureidkey="properties.ST_NM",
locationmode="geojson-id",
color="content_view",
scope="asia",
).data
)
# restrict to just India
fig_choropleth.update_geos(fitbounds="locations", visible=False)
fig_choropleth
输出