You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何保存go.Choropleth地图时放大地图且保持分辨率与画质

解决方法

要在保持750x750分辨率的前提下让地图本身占据更多画布空间,你可以通过调整Plotly地理图层的参数来实现,具体修改如下:

核心修改点

  • 移除地理边界的默认留白:通过fitbounds_padding参数消除地图与画布边缘之间的默认空白
  • 强制地理区域占满整个画布:设置domain参数让地图区域完全覆盖750x750的画布范围
  • 可选:手动控制地图缩放与中心:如果自动适配效果不佳,可直接指定地图中心坐标和缩放比例

修改后的完整代码

import typing as t
import plotly.graph_objects as go
import pandas as pd
import json
JSON = t.Union[str, int, float, bool, None,
               t.Mapping[str, 'JSON'], t.List['JSON']]


def draw_map(districts: JSON,
             df: pd.DataFrame,
             name: str,
             height: int,
             width: int) -> None:
    colorscale = [[0, '#D40000'],
                 [0.05789804908747634, '#D40000'],
                 [0.11705475141598479, '#CC2F4A'],
                 [0.12146003775959714, '#CC2F4A'],
                 [0.13089993706733785, '#E27F90'],
                 [0.18628067967275005, '#E27F90'],
                 [0.18753933291378216, '#F2B3BE'],
                 [0.18753933291378216, '#F2B3BE'],
                 [0.8193832599118943, '#86B6F2'],
                 [0.8445563247325362, '#86B6F2'],
                 [0.9030837004405287, '#4389E3'],
                 [0.9282567652611706, '#4389E3'],
                 [0.9616110761485211, '#1666CB'],
                 [1.0, '#1666CB']]
    fig = go.Figure(data=go.Choropleth(
        geojson=districts,
        locations=df["District"],
        z=df["Result"],
        colorscale=colorscale,
        showscale=False))
    fig.update_traces(marker_line_color="white",
                      marker_line_width=1)
    
    # 关键修改:调整地理图层参数
    fig.update_geos(
        fitbounds="locations",
        fitbounds_padding=0,  # 移除默认留白
        projection_type="natural earth",
        visible=False,
        bgcolor="#D7EAE2",
        domain={'x': [0, 1], 'y': [0, 1]}  # 让地图占满整个画布
    )
    
    fig.layout.paper_bgcolor = "#D7EAE2"
    fig.layout.plot_bgcolor = "#D7EAE2"
    fig.update_layout(margin_t=0, margin_b=0,
                      margin_l=0, margin_r=0)
    fig.write_image(name, width=width, height=height)
    return fig


def main() -> None:
    with open("districts.json") as file:
        districts = json.load(file)
    df = pd.read_csv("texas.csv", index_col=0)
    fig = draw_map(districts, df, "TX-20-pres-districts.png", 750, 750)

备选方案(自动适配不佳时使用)

如果自动适配的效果还是不满意,可以放弃fitbounds,手动指定德克萨斯州的中心坐标和缩放比例,确保地图填满画布:

fig.update_geos(
    center={'lat': 31.0, 'lon': -99.0},  # 德克萨斯州大致地理中心
    scale=5,  # 增大数值让地图放大,根据实际效果调整
    projection_type="natural earth",
    visible=False,
    bgcolor="#D7EAE2",
    domain={'x': [0, 1], 'y': [0, 1]}
)

参数说明

  • fitbounds_padding=0:消除Plotly为地图自动添加的周边空白,让地图尽可能贴近画布边缘
  • domain={'x': [0, 1], 'y': [0, 1]}:强制地理图层占据整个画布的x、y轴范围,避免隐藏的布局限制
  • 手动设置center和scale时,可根据实际生成的地图效果微调scale数值,直到地图刚好填满750x750的画布

内容的提问来源于stack exchange,提问作者Gleb Ryabov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 03:17:08