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

如何减小Plotly Express动态Choropleth地图的文件体积?

解决Plotly Choropleth动画重复GeoJSON导致文件过大的问题

Plotly Express的px.choropleth生成动画时,确实会为每个帧重复嵌入完整的GeoJSON数据,这是导致文件体积暴涨的核心原因。要实现GeoJSON复用,需要手动构建Figure对象,避免Express自动生成重复的GeoJSON trace。

具体实现步骤

  • 先创建一个基础的choropleth trace,仅加载一次GeoJSON,配置好地理相关参数(geojson、locations、featureidkey等)。
  • 为每个动画帧准备仅包含数值变化的数据(比如颜色对应的字段),不需要重复定义GeoJSON。
  • 构建动画帧(frames),每个帧只更新trace的数值属性(如z值),复用基础的GeoJSON结构。
  • 添加动画控制组件(滑块、播放按钮)到布局中。

代码示例

替换你原来的px.choropleth代码,改用如下手动构建方式:

import plotly.graph_objects as go

# 假设你的数据和GeoJSON已准备好:df, regionalized_data, disp_value, hover_data_present, readable_labels_local
animation_dates = []
if 'Inbetriebnahmemonat' in df.columns:
    animation_dates = sorted(df['Inbetriebnahmemonat'].unique())

# 1. 创建基础trace(仅加载一次GeoJSON)
base_trace = go.Choropleth(
    geojson=regionalized_data,
    locations=df[df['Inbetriebnahmemonat'] == animation_dates[0]]['statistische_kennziffer'],
    z=df[df['Inbetriebnahmemonat'] == animation_dates[0]][disp_value],
    featureidkey='properties.ARS',
    hover_data=hover_data_present,
    name='',
    colorbar_title=readable_labels_local.get(disp_value, disp_value)
)

# 2. 构建动画frames
frames = []
for date in animation_dates:
    frame_df = df[df['Inbetriebnahmemonat'] == date]
    frame = go.Frame(
        data=[go.Choropleth(
            # 仅更新需要变化的属性,复用GeoJSON
            locations=frame_df['statistische_kennziffer'],
            z=frame_df[disp_value],
            hover_data=hover_data_present
        )],
        name=str(date)
    )
    frames.append(frame)

# 3. 创建Figure并配置布局
fig = go.Figure(
    data=[base_trace],
    frames=frames,
    layout=go.Layout(
        fitbounds='locations',
        showlegend=False,
        basemap_visible=False,
        # 添加动画控制按钮
        updatemenus=[{
            'type': 'buttons',
            'buttons': [{
                'label': '播放',
                'method': 'animate',
                'args': [None, {
                    'frame': {'duration': 500, 'redraw': True},
                    'fromcurrent': True
                }]
            }, {
                'label': '暂停',
                'method': 'animate',
                'args': [[None], {
                    'frame': {'duration': 0, 'redraw': False},
                    'mode': 'immediate',
                    'transition': {'duration': 0}
                }]
            }],
            'direction': 'left',
            'pad': {'r': 10, 't': 87},
            'showactive': False,
            'x': 0.1,
            'xanchor': 'right',
            'y': 0,
            'yanchor': 'top'
        }],
        # 添加时间滑块
        sliders=[{
            'active': 0,
            'yanchor': 'top',
            'xanchor': 'left',
            'currentvalue': {
                'font': {'size': 20},
                'prefix': '时间:',
                'visible': True,
                'xanchor': 'right'
            },
            'transition': {'duration': 300, 'easing': 'cubic-in-out'},
            'pad': {'b': 10, 't': 50},
            'len': 0.9,
            'x': 0.1,
            'y': 0,
            'steps': [{
                'args': [[str(date)], {
                    'frame': {'duration': 300, 'easing': 'cubic-in-out', 'redraw': True},
                    'mode': 'immediate',
                    'transition': {'duration': 300}
                }],
                'label': str(date),
                'method': 'animate'
            } for date in animation_dates]
        }]
    )
)

# 调整悬停提示样式(可选)
fig.update_traces(hovertemplate='<br>'.join([f"{k}: %{{customdata[{i}]}}" for i, k in enumerate(hover_data_present.keys())]))

# 保存优化后的动画
fig.write_html('optimized_choropleth_animation.html')

原理说明

  • 基础trace中仅定义一次geojson,所有动画帧都复用这个GeoJSON结构,不再重复嵌入。
  • 每个动画帧只更新locations(匹配当前时间的区域)、z(颜色对应的数值)和hover_data,大幅减少单帧体积。
  • 手动配置的动画控制组件和px.choropleth生成的交互效果一致,但文件体积会显著降低。

额外优化建议

  • 继续压缩GeoJSON:用mapshaper工具对GeoJSON进行简化(删除冗余坐标、合并小多边形),进一步减小基础GeoJSON的体积。
  • 精简hover_data:只保留必要字段,避免冗余数据增加文件大小。

内容的提问来源于stack exchange,提问作者C Hecht

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 06:30:47