将图例添加到Geopandas [英] Add legend to geopandas
问题描述
我有一张智利地图( http://labgeo.ufro.cl/fichas/chile_geo/ficha_cl_geo.html 第一个链接为智利大陆",并希望将其绘制出来并添加一些我拥有经度和纬度数据的中心点.
I have a map of Chile (http://labgeo.ufro.cl/fichas/chile_geo/ficha_cl_geo.html first link that says "Chile continental) and would like to plot it and add some points of centers for which I have latitude and longitud data.
我是geopandas和matplotlib的新手,但是我使用来自这篇文章的matplotlib的建议答案设法绘制了以中心为不同颜色的圆点的地图:
I am newbie with geopandas and matplotlib but I managed to plot the map with the centers as dots of different colors using the suggested answer for matplotlib from this post: 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)
我无法尝试做其他事情的是出现的图例.
What I can´t make trying different things is the legend to appear.
- 我尝试添加legend = True
- 我尝试过先定义斧头来做,但是我无法正确地输入数据来创建绘图并最终一无所获.
- 尝试了此解决方案,但我的shp文件只有一行包含多面信息,并且我不知道如何创建建议的交叉数据框 为地理大熊猫图生成图例
- I have tried adding legend=True
- I have tried doing it through defining ax first but I can´t manage to feed the data correctly to create the plot and end up with nothing.
- Tried this solution but my shp file has only one row with multipolygon info and I don´t know how to create the crossed dataframe proposed Generating Legend for geopandas plot
到目前为止,我唯一要做的是通过在末尾添加.legend()来显示具有颜色编号的id的字典,如下所示:
geo_df_.plot(ax=map_.plot(figsize=(40, 30)), marker='o', c =geo_df_.Color, markersize=100).legend()
.
但是我得到了这个错误
So far the only thing I have managed to do is showing the dictionary of the ids with the color number by adding .legend() at the end as this:
geo_df_.plot(ax=map_.plot(figsize=(40, 30)), marker='o', c =geo_df_.Color, markersize=100).legend()
.
But I get this error
没有找到带有可放入图例的标签的手柄.
No handles with labels found to put in legend.
但是当我将颜色字典作为参数传递时,它将在图例中显示一个点.
but when I pass the color dictionary as an argument it would show one point in the legend.
我想实现的是这样一个传说:
What I would like to achieve is a legend as this:
摘自本文:控制ggplot2图例外观而不会影响绘图 我理想的传说是在侧面有一个正方形,并用它们代表的id中心标识所有有色点.例如,黄色圆点:(中心)5,紫色圆点:8,等等.
taken from this post: Control ggplot2 legend look without affecting the plot My ideal legend would be to have a square on the side with all the colored dots identified with the id center that they represent. So for example yellow dot: (center) 5, purple dot : 8, etc.
我管理的只是一个点,整个字典如下:
What I have manages is just one dot, that shows the entire dictionary as this:
推荐答案
请勿使用c
,而应使用column
.然后legend=True
将完成显示图例的技巧,而categorical=True
将为您提供所需的内容.在这种情况下,您可能会用完所有颜色,因此,如果要使所有颜色都不同(cmap=''
)
Do not use c
, but column
. And then legend=True
will do the trick to show the legend and categorical=True
will give you what you want. In this specific case you might run out of colors, so you will have to set another colormap if you want all colors different (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)
# 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
ax = map_.plot(figsize=(40, 30))
geo_df_.plot(ax=ax, marker='o', column='id', categorical=True,
markersize=100, legend=True, cmap='tab20')
这篇关于将图例添加到Geopandas的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!