如何在Plotly Python世界地图中移除南极洲?
如何在Plotly Choropleth地图中移除南极洲区域
要去掉地图里的南极洲区域,你可以通过调整Plotly的geo配置实现,以下两种方法都能解决问题:
方法1:使用natural earth投影
这种投影方式默认会隐藏南极洲,同时优化地图的整体显示比例,适合大多数场景:
修改代码中的geo配置部分:
import plotly.graph_objects as go go.Figure(data=go.Choropleth( locations=choc_df['ISO'], text=choc_df['company_location'], marker_line_color='black', marker_line_width=0.5, autocolorscale=False, colorscale='brbg_r', z=choc_df['rating'], customdata=choc_df['number'], hovertemplate="<br>".join([ "<b>%{text}</b>", "Number of brands: %{customdata}", "Rating: %{z}", ]) + '<extra></extra>', ), layout=dict(title='Rating', geo=dict( showcountries=True, countrycolor='#444444', showlakes=False, projection=dict(type='natural earth') # 关键配置:启用natural earth投影 ) ))
方法2:限制显示的纬度范围
如果你想保留原有投影样式,直接通过lataxis.range设定纬度区间,把南极洲(纬度低于-60°的区域)排除在外:
import plotly.graph_objects as go go.Figure(data=go.Choropleth( locations=choc_df['ISO'], text=choc_df['company_location'], marker_line_color='black', marker_line_width=0.5, autocolorscale=False, colorscale='brbg_r', z=choc_df['rating'], customdata=choc_df['number'], hovertemplate="<br>".join([ "<b>%{text}</b>", "Number of brands: %{customdata}", "Rating: %{z}", ]) + '<extra></extra>', ), layout=dict(title='Rating', geo=dict( showcountries=True, countrycolor='#444444', showlakes=False, lataxis=dict(range=[-60, 90]) # 关键配置:仅显示纬度-60°到90°的区域 ) ))
两种方案都能有效移除南极洲区域,第一种的地图比例更自然,第二种适合需要保留特定投影的场景。
内容的提问来源于stack exchange,提问作者whatserface
相关产品推荐
相关产品推荐

