我正试图用英国国家统计局的一些数据绘制一张伦敦的多层 map .

import geopandas as gpd
from shapely import wkt

# Convert the 'geometry' column to shapely geometry objects
london_wards_shp['geometry'] = london_wards_shp['geometry'].apply(wkt.loads)
london_wards_gpd = gpd.GeoDataFrame(london_wards_shp, geometry='geometry')
london_wards_gpd = london_wards_gpd.set_crs(epsg=4326)
london_wards_gpd.plot()

这是一个错误:

ValueError: aspect must be finite and positive 

我找到了一个绘图的解决方案:

london_wards_gpd.plot(aspect=1)

enter image description here

但后来我想用牛郎星来建立图层,一个有整个图层的 map .

> london_wards_map = alt.Chart(london_wards_gpd).mark_geoshape(
>     fill=None,  # No fill
>     stroke='darkgray',  # Black stroke
>     strokeWidth=1  # Stroke width ).encode(
>     tooltip='NAME:N'  # Replace 'NAME' with the actual name of the column that contains the ward names ).properties(
>     width=800,
>     height=600 ).project(
>     type='identity' )
> 
> hackney_wards = london_wards_gpd[london_wards_gpd['DISTRICT']
> =='Hackney']
> #hackney_wards = gpd.GeoDataFrame(hackney_wards, geometry='geometry')  # Convert DataFrame to GeoDataFrame
> #hackney_wards = hackney_wards.set_crs(epsg=4326) hackney_layer = alt.Chart(hackney_wards).mark_geoshape(
>     fill='lightgray',  # No fill
>     stroke='darkgray',  # Black stroke
>     strokeWidth=1  # Stroke width ).encode(
>     tooltip='NAME:N'  # Replace 'NAME' with the actual name of the column that contains the ward names ).properties(
>     width=800,
>     height=600 ).project(
>     type='identity' )

london_wards_map + hackney_layer

enter image description here

为什么是颠倒的?

我不太清楚如何诊断投影问题,

推荐答案

请阅读文档如何在笛卡尔坐标系中绘图:https://altair-viz.github.io/user_guide/marks/geoshape.html#cartesian-coordinates.

alt.Chart(gdf).mark_geoshape().project(
    type='identity',
    reflectY=True
)

输入几何体不投影,而是使用identity投影类型直接在原始坐标中渲染.您还必须定义reflectY,因为Canvas和SVG将正y视为指向下方.

Python相关问答推荐

如何使用matplotlib在Python中使用规范化数据和原始t测试值创建组合热图?

如何让剧作家等待Python中出现特定cookie(然后返回它)?

我们可以为Flask模型中的id字段主键设置默认uuid吗

如何在Python数据框架中加速序列的符号化

将tdqm与cx.Oracle查询集成

为什么\b在这个正则表达式中不解释为反斜杠

如何在两列上groupBy,并使用pyspark计算每个分组列的平均总价值

为什么在FastAPI中创建与数据库的连接时需要使用生成器?

循环浏览每个客户记录,以获取他们来自的第一个/最后一个渠道

pandas fill和bfill基于另一列中的条件

Django Table—如果项目是唯一的,则单行

Python—在嵌套列表中添加相同索引的元素,然后计算平均值

解决Geopandas和Altair中的正图和投影问题

提取最内层嵌套链接

为什么Visual Studio Code说我的代码在使用Pandas concat函数后无法访问?

如何将一个文件的多列导入到Python中的同一数组中?

仅取消堆叠最后三列

为什么按下按钮后屏幕的 colored颜色 保持不变?

查找数据帧的给定列中是否存在特定值

Sknowled线性回归()不需要迭代和学习率作为参数