Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add legend to geopandas

Tags:

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.

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

Here is my code:

#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.

  • 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

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:

enter image description here

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:

example of part of the map with the dictionary legend

like image 968
Isa Avatar asked Oct 15 '19 14:10

Isa


1 Answers

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)

#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

like image 97
martinfleis Avatar answered Oct 20 '22 09:10

martinfleis