Skip to main content
 首页 » 编程设计

python之GeoPandas 绘图功能不起作用

2024年10月01日11myhome

我有一个在 mapshaper 上看起来像这样的 shapefile:

但是当我尝试用下面的代码在 pandas 中绘制它时

police = gpd.read_file('srilanka_policestations') 
police.plot() 

jupyter notebook 给我一条错误消息“AttributeError: 'str' object has no attribute 'type'”。

我不确定哪里出了问题。我尝试绘制 GeoPandas 数据集“naturalearth_cities”,它工作正常。见下文:

geo-dataframe 在 pandas 中读取良好,但它不会绘制:

非常感谢任何帮助。谢谢你们!

请您参考如下方法:

geopandas GeoDataFrame 中的几何列不应包含字符串,而应包含实际的几何对象。 如果你有字符串,你可以按如下方式转换它们:

import shapely.wkt 
police['geometry'] = police['geometry'].apply(shapely.wkt.loads) 

但是,我不得不说以上是一种变通方法,就像您使用 geopandas read_file 读取 shapefile 一样,您不应该以字符串结尾。