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

Geopandas绘制全球choropleth地图时图像尺寸过大报错求助

解决Geopandas绘制全球Choropleth地图时的超大图像像素报错问题

问题描述

原始shapefile可正常绘制全球地图,但将化石燃料发电占比数据与几何数据合并后,绘图时触发报错:
Image size of 833x81232 pixels is too large. It must be less than 2^16 in each direction.
调整figsize参数无效,给boundary.plot()添加dpi参数时,又出现LineCollection.set() got an unexpected keyword argument 'dpi'错误。

解决方案

1. 检查并重置坐标参考系(CRS)

合并数据过程中可能意外修改了CRS,导致坐标轴范围异常放大。执行以下代码排查并修复:

# 查看当前数据的CRS
print(df_countries_shape_2.crs)
# 重置为全球通用的WGS84坐标系(EPSG:4326)
df_countries_shape_2 = df_countries_shape_2.to_crs(epsg=4326)

2. 清理异常几何对象

合并后的数据可能混入了面积异常的几何元素,拉宽了坐标轴范围。通过面积过滤清理:

# 计算每个几何对象的面积,过滤掉超出正常范围的对象
df_countries_shape_2['area'] = df_countries_shape_2.geometry.area
# 阈值根据全球地图调整,这里以1e8为例
df_countries_shape_2 = df_countries_shape_2[df_countries_shape_2['area'] < 1e8]

3. 手动限定坐标轴范围

如果CRS正常但坐标轴范围仍异常,直接设置全球经纬度的合理范围:

ax = df_countries_shape_2.boundary.plot(figsize=(10,10), edgecolor='black', linewidth=0.05)
# 限定全球地图的经纬度范围
ax.set_xlim([-180, 180])
ax.set_ylim([-90, 90])
df_countries_shape_2.plot(ax=ax, column='Fossil fuels - % electricity', legend=True, cmap='RdBu')

4. 正确设置dpi参数

dpi参数不能传递给boundary.plot(),需要在创建Figure时指定:

import matplotlib.pyplot as plt
# 创建Figure时设置dpi
fig, ax = plt.subplots(figsize=(10,10), dpi=40)
df_countries_shape_2.boundary.plot(ax=ax, edgecolor='black', linewidth=0.05)
df_countries_shape_2.plot(ax=ax, column='Fossil fuels - % electricity', legend=True, cmap='RdBu')

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 21:02:35