[SOLVED] geopandas.explore – How to plot more than one layer?

Issue

This Content is from Stack Overflow. Question asked by Sofi1101

I have one dataset with polygons geometry and another with points, I want to plot both in the same graphic with geopandas.explore function but I get the error ‘Map’ object has no attribute ‘set_aspect’

The code is:

import geopandas as gpd
import matplotlib.pyplot as plt

f, ax= plt.subplots(1, figsize=(12,5)) 

Polygons= df.explore('column', scheme='Quantiles', cmap= 'GnBu', k=6)

points= df_points.plot(ax= Polygons)

plt.show()```



Solution

  • have used built in sample geometry to create MWE
  • your core issue is that you are mixing folium and matplotlib. These are two completely independent libraries
  • simple case of taking folium map returned from plotting polygons and then plotting another layer on this map by passing it to explore() call for points
import geopandas as gpd
import folium

df = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres")).loc[lambda d: d["continent"].eq("Africa")]
df_points = gpd.read_file(gpd.datasets.get_path("naturalearth_cities"), mask=df)

m = df.explore(height=250, width=500, name="Polygons")
m = df_points.explore(m=m, color="red", name="Points")
# this is completely optional
folium.LayerControl().add_to(m)

m

enter image description here


This Question was asked in StackOverflow by Sofi1101 and Answered by Rob Raymond It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?