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

Question:

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()```
Asked By: Sofi1101

||

Answers:

  • 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

Answered By: Rob Raymond
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.