在 GeoPandas 中添加图例

8

我有一份智利地图(http://labgeo.ufro.cl/fichas/chile_geo/ficha_cl_geo.html 第一个链接写着“智利大陆”),我想绘制它并在其中添加一些中心点,这些中心点的纬度和经度数据已知。

我是geopandas和matplotlib的新手,但我成功地使用这篇文章中matplotlib的建议答案将地图与不同颜色的点作为中心绘制出来:Color by Column Values in Matplotlib

以下是我的代码:

#Loading data, since I am making the coordinates up they won´t fit the map nicely but you will get the idea

map_= gpd.read_file("cl_continental_geo.shp")
geo_df_ = pd.DataFrame({"id":np.random.randint(20, size=133) ,"Latitude": np.random.normal(-34.406922,7.819504, 133), "Longitud": np.random.normal(-71.243350,1.254126, 133)})

geometry =[Point(xy) for xy in zip( geo_df_["Longitud"],geo_df_["Latitude"])]
geo_df_ =gpd.GeoDataFrame(geo_df_, crs={"init":"epsg:4326"},geometry= geometry)

# creating color map for categories
categories = np.unique(geo_df_["id"])
colors = np.linspace(0, 1, len(categories))
colordict = dict(zip(categories, colors))

#matching it to the geopandas df
geo_df_["Color"] = geo_df_["id"].apply(lambda x: colordict[x])

#plotting    
geo_df_.plot(ax=map_.plot(figsize=(40, 30)), marker='o', c =geo_df_.Color, markersize=100)

我试图尝试不同的方法,但无法使图例显示出来。
- 我尝试添加legend=True。 - 我尝试通过首先定义ax,但我无法正确地传递数据来创建图表,最终什么也没有。 - 尝试了这个解决方案,但是我的shp文件只有一行多边形信息,我不知道如何创建所建议的交叉数据框 Generating Legend for geopandas plot 到目前为止,我唯一做到的是显示带有颜色编号的ID字典,通过在结尾处添加 .legend() 来实现此操作: geo_df_.plot(ax=map_.plot(figsize=(40,30)), marker='o', c=geo_df_.Color, markersize=100).legend()。 但我会收到以下错误:
No handles with labels found to put in legend.
但当我将颜色字典作为参数传递时,它会显示一个点在图例中。
我想要实现的是像这个帖子中的图例: enter image description here 取自此帖子:Control ggplot2 legend look without affecting the plot
我的理想图例将是在侧面有一个带有所有彩色点的正方形,通过它们所代表的中心标识。因此,例如黄点:(中心)5,紫点:8等。
我已经做到的只是一个点,显示整个字典,如下所示: example of part of the map with the dictionary legend

如果在代码末尾添加 plt.legend() 会发生什么? - Juan C
它会显示“没有带标签的句柄”。如果我在绘图函数中添加label = geo_df_.Color,然后再添加plt.legend(),它会打印出ID的颜色字典和颜色编号。 - Isa
然后它会给我一个颜色点和类别列表。它会打印图例,但不包括所有的颜色点和映射。 - Isa
你能否在你的问题中添加一张图片以更好地理解问题? - Juan C
我以为我没有上传图片的特权。在图片中,您可以看到地图的一部分和中心点,右侧有图例,只有一个点和整个字典信息。我要么得到这个,要么得到同一个点和一些整数。 - Isa
显示剩余3条评论
1个回答

10

不要使用c,而是使用column。然后使用legend=True可以显示图例,categorical=True将给你想要的结果。在这种特定情况下,你可能会用完颜色,所以如果你想要所有的颜色都不同(cmap=''),你需要设置另一个颜色映射。

map_= gpd.read_file("/Users/martin/Downloads/cl_continental_geo/cl_continental_geo.shp")
geo_df_ = pd.DataFrame({"id":np.random.randint(20, size=133) ,"Latitude": np.random.normal(-34.406922,7.819504, 133), "Longitud": np.random.normal(-71.243350,1.254126, 133)})

geometry =[Point(xy) for xy in zip( geo_df_["Longitud"],geo_df_["Latitude"])]
geo_df_ =gpd.GeoDataFrame(geo_df_, crs={"init":"epsg:4326"},geometry= geometry)

#plotting    
ax = map_.plot(figsize=(40, 30))
geo_df_.plot(ax=ax, marker='o', column='id', categorical=True,
             markersize=100, legend=True, cmap='tab20')

enter image description here


嘿,感谢您抽出时间。问题是颜色字典中的值并不代表定量测量,它们是我根据另一篇帖子生成的数字,以为每个唯一的中心ID赋予颜色。我需要的是一个图例,让我可以在地图上可视化特定中心的位置。因此,我需要一个将中心代码与点的颜色相对应的映射表,而不是颜色条。 - Isa
我不认为我理解了。你提供的数据和上面的代码片段中,哪一列存储了你想要绘制的值?我很愿意帮忙,但我不知道你试图实现什么。 - martinfleis
我想绘制的是智利地图上中心位置的位置。颜色将指示中心的ID。我在最后编辑了我的问题,添加了我想要获得的示例。 - Isa
geo_df_.plot(ax=ax, marker='o', column='id', categorical=True, markersize=100, legend=True) - martinfleis
就是这样!唯一的问题是图例中的颜色重复了,因此对于每三个中心,它使用相同的颜色。您能否更新您的问题并包含这行文字,以便我可以接受您的答案。谢谢。 - Isa
编辑了一个简短的解释,以说明重复颜色。 - martinfleis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接