【问题标题】:Having issues while annotating labels on map with different projections using matplotlib and geopandas使用 matplotlib 和 geopandas 在地图上用不同的投影注释标签时遇到问题
【发布时间】:2021-08-11 11:09:48
【问题描述】:

我正在使用默认地图,例如:

world = geopandas.read_file(gpd.datasets.get_path('naturalearth_lowres'))

我可以使用以下 for 循环示例从另一个 GeoDataFrame(此处称为 sample_gdf)成功地将标签注释到此地图:

for idx, row in sample_gdf.iterrows():
     plt.annotate(text=row['country_name'], # e.g. this column contains the names of each countries
                  xy=(row['longitude'], row['latitude']), # e.g. these columns are showing the coordinates of middle points of each countries
                  horizontalalignment='center')

This is how it looks like with epsg=4326 当我想更改地图的投影时,问题就开始了。上面变量“世界”的默认 CRS 是 epsg:4326。只要我像这样更改投影:

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world = world.to_crs(epsg=3035)

专注于欧洲,我的标签不再出现在正确的位置。我一直在寻找解决此问题的建议一周,但目前找不到任何解决方案。谢谢你的帮助。 And this is how it looks like with epsg=3035 标签出现在左下角。

【问题讨论】:

    标签: python matplotlib coordinates geopandas annotate


    【解决方案1】:

    这一切都归结为您用于定位注释的['longitude'], ['latitude'] 列中存储的内容。您需要确保它们在正确的投影中,因为坐标需要反映绘图上使用的实际单位。

    重投影后,获取新坐标并使用这些坐标。

    sample_gdf["x"] = sample_gdf.centroid.x
    sample_gdf["y"] = sample_gdf.centroid.y
    
    sample_gdf.plot()
    
    for idx, row in sample_gdf.iterrows():
         plt.annotate(text=row['country_name'], # e.g. this column contains the names of each countries
                      xy=(row['x'], row['y']), # e.g. these columns are showing the coordinates of middle points of each countries
                      horizontalalignment='center')
    

    【讨论】:

    • 我解决了这个问题。当我创建我的sample_gdf 时,我设置了crs=3050,因为我想要这个投影。但是现在我意识到在创建 GeoDataFrame (sample_gdf) 时,必须根据坐标值将 crs 设置为默认值。然后把crs改成sample_gdf.to_crs(epsg=3050, inplace=True)就可以正常使用了。
    猜你喜欢
    • 1970-01-01
    • 2017-07-30
    • 1970-01-01
    • 2013-09-14
    • 2017-09-11
    • 1970-01-01
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多