如何添加静态标签来巧妙地表达“choropleth_mapbox”?
How to add static labels to plotly express `choropleth_mapbox`?
使用当前代码,我得到了悬停工具提示,但是这些在 PNG 导出时不可见。我需要在每个多边形上都有静态标签。
import plotly.express as px
df = px.data.election()
geojson = px.data.election_geojson()
fig = px.choropleth_mapbox(df, geojson=geojson, color="Bergeron",
locations="district", featureidkey="properties.district",
center={"lat": 45.5517, "lon": -73.7073},
mapbox_style="carto-positron", zoom=9)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
我发现的解决方案不起作用,它们仅适用于 choropleth
但不适用于我想使用的 choropleth_mapbox
。他们的名单:
https://community.plotly.com/t/labels-inside-counties-for-a-map/13953
https://community.plotly.com/t/annotations-on-plotly-choropleth/36219
https://community.plotly.com/t/how-can-i-change-the-hover-labels-into-static-labels/4690/2
- 显而易见的答案是添加一个 Scattermapbox 图层,其中包含您想要的文本。这被这个bug. I did not find an clear replacement for carto-positron as a replacement on https://www.mapbox.com复杂化了,所以地图样式改变了
- 在https://account.mapbox.com to get a token, create a base map on https://studio.mapbox.com上创建一个帐户以创建底图
- 也尝试使用 plotly annotations。但是我找不到一种方法来获取地图的边界框,以便能够将 lat/lot 坐标转换为域坐标 (0-1)
mapboxtoken="****"
mapboxstyle="mapbox://styles/***"
- 使用 geopandas 来简单访问
centroid
的几何图形
import plotly.express as px
import plotly.graph_objects as go
import geopandas as gpd
df = px.data.election()
geojson = px.data.election_geojson()
gdf = (
gpd.GeoDataFrame.from_features(geojson)
.merge(df, on="district")
.assign(lat=lambda d: d.geometry.centroid.y, lon=lambda d: d.geometry.centroid.x)
.set_index("district", drop=False)
)
# for convenience of rebuilding and adding traces...
def basemap():
fig = px.choropleth_mapbox(
df,
geojson=geojson,
color="Bergeron",
locations="district",
featureidkey="properties.district",
center={"lat": 45.5517, "lon": -73.7073},
mapbox_style=mapboxstyle,
# mapbox_style="carto-positron",
zoom=9,
)
fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0},
mapbox={"accesstoken":mapboxtoken}
)
return fig
齐心协力
texttrace = go.Scattermapbox(
lat=gdf.geometry.centroid.y,
lon=gdf.geometry.centroid.x,
text=gdf["Bergeron"].astype(str),
textfont={"color":"white","size":20, "family":"Courier New"},
mode="text",
name="Bergeron"
)
basemap().add_trace(texttrace)
输出
使用当前代码,我得到了悬停工具提示,但是这些在 PNG 导出时不可见。我需要在每个多边形上都有静态标签。
import plotly.express as px
df = px.data.election()
geojson = px.data.election_geojson()
fig = px.choropleth_mapbox(df, geojson=geojson, color="Bergeron",
locations="district", featureidkey="properties.district",
center={"lat": 45.5517, "lon": -73.7073},
mapbox_style="carto-positron", zoom=9)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
我发现的解决方案不起作用,它们仅适用于 choropleth
但不适用于我想使用的 choropleth_mapbox
。他们的名单:
https://community.plotly.com/t/labels-inside-counties-for-a-map/13953
https://community.plotly.com/t/annotations-on-plotly-choropleth/36219
https://community.plotly.com/t/how-can-i-change-the-hover-labels-into-static-labels/4690/2
- 显而易见的答案是添加一个 Scattermapbox 图层,其中包含您想要的文本。这被这个bug. I did not find an clear replacement for carto-positron as a replacement on https://www.mapbox.com复杂化了,所以地图样式改变了
- 在https://account.mapbox.com to get a token, create a base map on https://studio.mapbox.com上创建一个帐户以创建底图
- 也尝试使用 plotly annotations。但是我找不到一种方法来获取地图的边界框,以便能够将 lat/lot 坐标转换为域坐标 (0-1)
mapboxtoken="****"
mapboxstyle="mapbox://styles/***"
- 使用 geopandas 来简单访问
centroid
的几何图形
import plotly.express as px
import plotly.graph_objects as go
import geopandas as gpd
df = px.data.election()
geojson = px.data.election_geojson()
gdf = (
gpd.GeoDataFrame.from_features(geojson)
.merge(df, on="district")
.assign(lat=lambda d: d.geometry.centroid.y, lon=lambda d: d.geometry.centroid.x)
.set_index("district", drop=False)
)
# for convenience of rebuilding and adding traces...
def basemap():
fig = px.choropleth_mapbox(
df,
geojson=geojson,
color="Bergeron",
locations="district",
featureidkey="properties.district",
center={"lat": 45.5517, "lon": -73.7073},
mapbox_style=mapboxstyle,
# mapbox_style="carto-positron",
zoom=9,
)
fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0},
mapbox={"accesstoken":mapboxtoken}
)
return fig
齐心协力
texttrace = go.Scattermapbox(
lat=gdf.geometry.centroid.y,
lon=gdf.geometry.centroid.x,
text=gdf["Bergeron"].astype(str),
textfont={"color":"white","size":20, "family":"Courier New"},
mode="text",
name="Bergeron"
)
basemap().add_trace(texttrace)