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

芝加哥区域分级统计图异常倾斜问题排查与修复求助

问题原因
  1. 投影配置缺失:你的GeoDataFrame使用的是经纬度地理坐标系(EPSG:4326),但Altair的mark_geoshape默认会把球面经纬度直接当作平面坐标渲染,没有应用适配的地图投影,导致地图出现倾斜、拉伸变形。
  2. CRS操作错误:代码中joined_data = gpd.sjoin(...).set_crs(epsg=4326)的set_crs用法有误——set_crs是给无坐标系的数据赋予CRS,而非转换坐标系。如果wards_gpd原本的CRS不是4326,这一步会强制错误赋值,加剧坐标不匹配问题。
修复方案

步骤1:修正CRS转换逻辑

将错误的set_crs替换为to_crs,确保所有GeoDataFrame统一使用EPSG:4326坐标系:

# 先将辖区边界数据转换为EPSG:4326
wards_gpd = wards_gpd.to_crs(epsg=4326)

# 创建检查点GeoDataFrame时直接指定正确CRS
geometry = [Point(lon, lat) for lon, lat in zip(inspections_df['LONGITUDE'], inspections_df['LATITUDE'])]
inspections_gdf = gpd.GeoDataFrame(inspections_df, geometry=geometry, crs="EPSG:4326")

# 空间连接无需额外强制设置CRS
joined_data = gpd.sjoin(wards_gpd, inspections_gdf, how='inner', op='contains')

步骤2:为Altair图表指定投影

在图表配置中通过project参数添加适配的地图投影(推荐使用albersUsa适配美国本土,或mercator通用投影):

choropleth_map_Inspections = alt.Chart(inspections_connected).mark_geoshape(stroke='white',
    strokeWidth=0.6).encode(
    color=alt.Color('Inspections:Q', scale=alt.Scale(zero = False, scheme='inferno')),
    tooltip=['ward','Inspections']
).properties(
    width=600,
    height=400,
    title='Inspections in every Chicago Ward'
).project(
    type='albersUsa'  # 适配美国本土的投影,避免倾斜变形
)

choropleth_map_Inspections

额外优化:处理空值

由于使用how='outer'合并数据,部分辖区可能无检查记录,建议填充空值避免图表异常:

inspections_connected['Inspections'] = inspections_connected['Inspections'].fillna(0)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 18:05:27